【问题标题】:Binding float data type with UpdateSourceTrigger set toPropertyChanged in WPF在 WPF 中将带有 UpdateSourceTrigger 设置为PropertyChanged 的​​浮点数据类型绑定
【发布时间】:2013-07-29 07:21:54
【问题描述】:

我在 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 没有帮助,因为小数可以放在任何地方。 任何帮助都会很棒。

【问题讨论】:

  • this 对你没有帮助?
  • StringFormat 限制为提到的小数位,例如 StringFormat='{}{0:F2}' 限制为两位小数
  • 如果你的 TextBox 将 char 的数量限制为 7,如果我理解你的话,只需执行 StringFormat='{}{0:F6},你应该得到你想要的

标签: wpf


【解决方案1】:

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

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

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

【讨论】:

  • 你说得对,StringFormat='{}{0:F2}' 不能解决这个问题。您必须编写转换器,根据小数点前的位数对浮点数进行四舍五入。
【解决方案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
}

【讨论】:

    猜你喜欢
    • 2010-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多