How can I bind a string to double in WPF?

守給你的承諾、 提交于 2019-12-12 19:02:49

问题


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>

回答1:


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>



回答2:


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.




回答3:


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?



来源:https://stackoverflow.com/questions/845281/how-can-i-bind-a-string-to-double-in-wpf

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!