WPF Binding Text Block Text

TextBlock binding does not work, and I can not understand why ...

(This code works, but TextBlock is not updated)

Xaml

<TextBlock x:Name="filterAllText"
 Text="{Binding UpdateSourceTrigger=PropertyChanged}" />

Codebehind

filterAllText.DataContext = LogSession.test.MyCoynt;

WITH#

public class Test : INotifyPropertyChanged {
 public int myCoynt;

     public int MyCoynt {
        get { return myCoynt; }
        set {
            myCoynt = value;
            NotifyPropertyChanged();
        }
    }

     public event PropertyChangedEventHandler PropertyChanged;

     protected virtual void NotifyPropertyChanged(
        [CallerMemberName] String propertyName = "") {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null) {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
}
+5
source share
2 answers

Try the following:

<TextBlock x:Name="filterAllText" 
    Text="{Binding UpdateSourceTrigger=PropertyChanged, Path=MyCoynt}" />

And set your DataContextas:

filterAllText.DataContext = LogSession.test;
+9
source
<TextBlock x:Name="filterAllText" Text="{Binding Path=., UpdateSourceTrigger=PropertyChanged}" />

this should work, but this is not an ordinary way

EDIT: the best way is an underder from Goanne

0
source

All Articles