2

在 WPF 中使用 UpdateSourceTrigger 时,我遇到了浮点数据类型的问题。我有一个浮点数据类型的属性,它被绑定到一个 TextBox 并将其绑定的 UpdateSourceTrigger 设置为 PropertyChanged,但 WPF 不允许我键入“。 ' 在文本框中,除非我将 UpdateSourceTrigger 更改为 LostFocus。我认为这是因为我们无法键入“。” 在浮点值的末尾。我不知道如何修复它,因为我需要输入“。” 并将 UpdateSourceTrigger 设置为 PropertyChanged。

我设计的文本框只能占用 7 个字符,例如 1) 12.3456 2) 1234.56 等

属性是:`

 public float? Expenditure
{
    get;set;
}

在 XAML 中:

<TextBox Text="{Binding Expenditure, UpdateSourceTrigger=PropertyChanged}"/>

StringFormat 无济于事,因为可以将小数放在任何地方。任何帮助都会很棒。

4

2 回答 2

2

只需更改绑定的 StringFormat 属性以显示属性的两位小数:

<TextBox Text="{Binding Expenditure, UpdateSourceTrigger=PropertyChanged, StringFormat='{}{0:F2}'}"/>

您也可以编写自定义 FloatToStringConverter (是一个示例)。您自己的浮点到字符串和字符串到浮点的转换方法将允许您处理 TextBox 的空文本字段并将其转换为 null。

于 2013-07-29T08:34:45.920 回答
2

我写了一个值转换器,可以解决你的问题:

用法:

<Window x:Class="BindingExample.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:BindingExample"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <local:DoubleToStringConverter x:Key="DoubleToStringConverter" DigitsCount="5"/>
    </Window.Resources>
    <StackPanel>
        <TextBox Text="{Binding FloatProperty, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource DoubleToStringConverter}}" Margin="5"/>
    </StackPanel>
</Window>

转换器:

[ValueConversion(typeof(double), typeof(string))]
public class DoubleToStringConverter : IValueConverter
{
    #region IValueConverter Members

    public DoubleToStringConverter()
    {
        // Default value for DigitsCount
        DigitsCount = 7;
    }

    // Convert from double to string
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (value == null)
            return null;

        double doubleValue = System.Convert.ToDouble(value);

        // Calculate digits count
        int digitsBeforePoint = System.Convert.ToInt32(Math.Ceiling(Math.Log10(doubleValue)));
        int digitsAfterPoint = DigitsCount - digitsBeforePoint;

        // TODO: You have to handle cases where digitsAfterPoint < 0

        // Create formatString that is used to present doubleValue in desired format     
        string formatString = String.Format("{{0:F{0}}}", digitsAfterPoint);

        return String.Format(formatString, doubleValue);
    }

    // Convert from string to double
    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (value == null)
            return null;

        double? result = null;

        try
        {
            result = System.Convert.ToDouble(value);
        }
        catch
        {
        }

        return result.HasValue ? (object)result.Value : DependencyProperty.UnsetValue;
    }

    public int DigitsCount { get; set; }

    #endregion
}
于 2013-07-29T09:29:39.743 回答