TextBox为空时,数据绑定不会更新

时间:2010-02-09 05:17:08

标签: .net wpf data-binding properties string

我正在使用属于float类型的属性绑定TextBox。一切正常,我在TextBox中更改了值,并在属性中更新。当我将TextBox设为空白,我的属性没有更新,它仍然具有旧值时,会出现问题。现在我需要在绑定中使用转换器,以便在TextBox中出现空值时使用默认值更新属性。我想知道为什么会这样做?还有其他解决办法吗?

3 个答案:

答案 0 :(得分:9)

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

第一种方法是添加一个类型为string的属性,用它绑定TextBox并实现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的第一种方式。

答案 1 :(得分:7)

只需像这样更改Binding

<TextBlock Text={Binding Path=Name, TargetNullValue='',UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}/>

答案 2 :(得分:0)

我认为问题在于绑定系统与空TextBox匹配的内容。对你来说它可能是零,但对于其他人来说,它可能是Single.NegativeInfinity。