Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Data Binding doesn't update when TextBox is Empty

I am Binding a TextBox with a property which is of type float. Everything works fine, I change the value in TextBox and it gets updated in property. The problem occurs when I make the TextBox blank, my property doesn't get updated, it is still having old value. Now I need to use converter in my binding to update property with default value in case of blank value from TextBox. I want to know Why this behavior? Is there any other solution to this?

like image 946
viky Avatar asked Feb 09 '10 05:02

viky


1 Answers

Your property is not updating because it's not possible to convert empty string to float. There are two ways to solve this.

First way is to add a property which type is string, bind the TextBox with it and implement changing of float property. Like this:

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

Second way is to use a converter:

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>

There is an article about converters

I prefer the first way with MVVM pattern.

like image 138
bniwredyc Avatar answered Nov 15 '22 06:11

bniwredyc