tags:

views:

845

answers:

2

I want to set a binding. The problem is that the target is of type string but the source is of type double. In the following code VersionNumber is of type double. When I run this, the textblock is empty, without throwing any exceptions. How can I set this binding?

<Style TargetType="{x:Type MyControl}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type MyControl}">
                <TextBlock Text="{TemplateBinding Property=VersionNumber}" />
            </ControlTemplate>
        </Setter.Value>
    </Setter>        
</Style>
+2  A: 

You need a converter:

namespace Jahedsoft
{
    [ValueConversion(typeof(object), typeof(string))]
    public class StringConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return value == null ? null : value.ToString();
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}

Now you can use it like this:

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:j="clr-namespace:Jahedsoft">

    <j:StringConverter x:Key="StringConverter" />
</ResourceDictionary>

...

<Style TargetType="{x:Type MyControl}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type MyControl}">
                <TextBlock Text="{TemplateBinding Property=VersionNumber, Converter={StaticResource StringConverter}}" />
            </ControlTemplate>
        </Setter.Value>
    </Setter>        
</Style>
M. Jahedbozorgan
You do not need a converter for this, if you are willing to accept the .ToString Format, or if you have SP1 and can use the StringFormat property of the Binding. The problem is elsewhere.
Scott Weinstein
This is a generic method which of cource works without SP1.
M. Jahedbozorgan
+1  A: 

You don't need a ValueConverter. Double to String targets work just fine. As a rule, Binding will call ToString() as a last resort.

Here's an example:

<Window x:Class="WpfApplication1.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        x:Name="w1">
        <TextBlock Text="{Binding Path=Version, ElementName=w1}" />
</Window>
using System;
using System.Windows;
namespace WpfApplication1
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }
        public double Version { get { return 2.221; } }
    }
}

The problem is probably that your Binding source isn't what you think it is. As a rule binding failures in WPF do not raise exceptions. They do log their failures however. See How can I debug WPF bindings?

Scott Weinstein
Double to String targets DO NOT work.
Moheb, Try the simple example. Double to string is very common and is done all the time
Scott Weinstein