Changing value in setter property when using two-way WPF data binding

I have a TextBox that is bound to the text property of an Entity object. I would like to be able to reformat the text that the user enters in some cases - for example, if the user enters "2/4" (share) - I would like to change this to "1/2".

Through the “specified part” of the text property, I can change the value of the Entity object, but this does not appear in the TextBox - does it still read “2/4”?

+5
source share
2 answers

, WPF "", TextBox, , PropertyChanged .

TextBox , :

textBox.GetBindingExpression(TextBox.TextProperty).UpdateTarget();

, . , , TextBox. , TextBox, PropertyChanged , .

, , , , , .


! IsAsync = true:

<TextBox x:Name="textBox" Text="{Binding Path=TestData, IsAsync=true}"/>

, , PropertyChanged, .


(32 ) .NET 4, IsAsync.

+15

INotifyPropertyChanged ?

    private string _fraction;

    public string Fraction
    {
        get { return _fraction; }
        set
        {
            _fraction = ReduceFraction(value);
            NotifyPropertyChanged("Fraction");
        }
    }

    private string ReduceFraction(string value)
    {
        string result = "1/2";
        // Insert reduce fraction logic here
        return result;
    }


    public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged(String info)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(info));
        }
    }
0

All Articles