views:

595

answers:

3

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?

A: 

I think the problem would be to what the binding system match an empty TextBox. For you it might be zero but for someone else it might be Single.NegativeInfinity.

serialhobbyist
+2  A: 

just change the Binding like this

<TextBlock Text={Binding Path=Name, TargetNullValue='',UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}/>
Kishore Kumar
who does this work, e.g you have set the TargetNullValue to a string not a float?
Ian Ringrose
This was added in .net3.5 sp1 apparently. I wasn't aware of it until now. Cheers!
Daniel
fine, keep the gud work buddy
Kishore Kumar
+1  A: 

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.

bniwredyc
I got your point, thanks for the answer!!
viky