【问题标题】:How can I bind a string to double in WPF?如何在 WPF 中将字符串绑定为双精度值?
【发布时间】:2009-05-10 12:54:01
【问题描述】:

我想设置一个绑定。问题是目标是字符串类型,但源是双精度类型。 在下面的代码中,VersionNumber 是 double 类型。当我运行它时,文本块是空的,没有抛出任何异常。 如何设置此绑定?

<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>

【问题讨论】:

  • 您绑定到的 VersionNumber 属性是否可能不是双精度的,这可以解释为什么您没有看到预期的绑定行为?

标签: wpf binding


【解决方案1】:

你需要一个转换器:

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();
        }
    }
}

现在你可以像这样使用它了:

<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>

【讨论】:

  • 如果您愿意接受 .ToString 格式,或者如果您有 SP1 并且可以使用 Binding 的 StringFormat 属性,则不需要转换器。问题出在其他地方。
【解决方案2】:

示例中存在细微差别。

如果您直接在 Window 或其他控件中使用 Texblock,则将 double 绑定到 Text 属性可以正常工作,因为它默认返回 ToString() 方法,但是如果您尝试在控制模板。

那么你需要一个像帖子中建议的转换器。

【讨论】:

    【解决方案3】:

    您不需要 ValueConverter。 Double to String 目标工作得很好。通常,Binding 将调用 ToString() 作为最后的手段。

    这是一个例子:

    <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; } }
        }
    }
    

    问题可能是您的 Binding 源不是您认为的那样。 作为规则,WPF 中的绑定失败不会引发异常。然而,他们确实记录了他们的失败。见How can I debug WPF bindings?

    【讨论】:

    • Moheb,试试这个简单的例子。 double to string 很常见,而且一直都在做
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-16
    • 1970-01-01
    • 2011-03-22
    • 2010-10-03
    • 1970-01-01
    相关资源
    最近更新 更多