Moheb
Moheb

Reputation:

How can I bind a string to double in WPF?

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>

Upvotes: 2

Views: 12679

Answers (3)

Scott Weinstein
Scott Weinstein

Reputation: 19117

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?

Upvotes: 3

G&#246;ran
G&#246;ran

Reputation: 41

There is a small difference in the examples.

Binding a double to the Text property works fine if you are using a Texblock directly in Window or another control, since it defaults back to the ToString() method, BUT this doesn't work if you try to use it in a ControlTemplate.

Then you need a Converter like it was suggested in the post.

Upvotes: 4

M. Jahedbozorgan
M. Jahedbozorgan

Reputation: 7054

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>

Upvotes: 5

Related Questions