.net - 当 TextBox 为空时,数据绑定(bind)不会更新

标签 .net wpf data-binding properties string

我正在绑定(bind)一个具有浮点类型属性的文本框。一切正常,我更改了 TextBox 中的值,它在属性中得到更新。当我将 TextBox 设为空白时会出现问题,我的属性没有得到更新,它仍然具有旧值。现在我需要在绑定(bind)中使用转换器来使用默认值更新属性,以防 TextBox 出现空白值。我想知道为什么会出现这种行为?有没有其他解决方案?

最佳答案

您的属性未更新,因为无法将空字符串转换为浮点数。有两种方法可以解决这个问题。

第一种方法是添加一个类型为字符串的属性,将TextBox与它绑定(bind)并实现 float 属性的更改。像这样:

public partial class Window1 : Window, INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    public Window1()
    {
        InitializeComponent();
        // don't use this as DataContext, 
        // it's just an example
        DataContext = this;
    }

    private float _FloatProperty;
    public float FloatProperty
    {
        get { return _FloatProperty; }
        set
        {
            _FloatProperty = value;
            OnPropertyCahnged("FloatProperty");
        }
    }

    private string _StringProperty;
    public string StringProperty
    {
        get { return _StringProperty; }
        set
        {
            _StringProperty = value;
            float newFloatValue;

            // I think you want 0 when TextBox is empty, right?
            FloatProperty = float.TryParse(_StringProperty, out newFloatValue) ? newFloatValue : 0;

            OnPropertyCahnged("StringProperty");
        }
    }

    protected void OnPropertyCahnged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs("StringProperty"));
        }
    }
}

第二种方法是使用转换器:
namespace WpfApplication3
{
    public partial class Window1 : Window, INotifyPropertyChanged
    {
        public static readonly IValueConverter TextBoxConverter = new FloatConverter();

        /* code from previous example without StringProperty */

    }

    public class FloatConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return value.ToString();
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            float f;
            if (value is string && float.TryParse(value as string, out f))
            {
                return f;
            }

            return 0f;
        }
    }
}

XAML:
<Window x:Class="WpfApplication3.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:WpfApplication3="clr-namespace:WpfApplication3">
    <Grid>
        <TextBox Text="{Binding FloatProperty,  Converter={x:Static WpfApplication3:Window1.TextBoxConverter}}" />
    </Grid>
</Window>

an article about converters

我更喜欢 MVVM pattern 的第一种方式.

关于.net - 当 TextBox 为空时,数据绑定(bind)不会更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2226911/

相关文章:

.NET SDK 项目类型引用问题

C# 资源文件 - 如何从 XAML 访问内部资源?

c# - AvalonDock MVVM 锚定位置

C# WPF BorderBrush 不会设置颜色

c# - 在 WPF 的组合框中显示 MySQL 数据库的列?我只显示 'System.Data.DataRowView'

c# - WPF 中的绑定(bind)字典

cocoa-touch - 是否有适用于 iOS 的数据绑定(bind)机制?

c# - .net FlagsAttribute 枚举是否需要手动赋值?

c# - 通过 C# 第四版的 CLR。 - 对等待任务死锁感到困惑

.net - 在 DataTemplate 中使用 TextSearch.Text