WPF TextBlock text Binding

37,488

Try this:

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

And set your DataContext like:

filterAllText.DataContext = LogSession.test;
Share:
37,488
persianLife
Author by

persianLife

Updated on July 19, 2022

Comments

  • persianLife
    persianLife almost 2 years

    the TextBlock binding does not work and I cant figure why...

    (This Code Works but the TextBlock does not get Updated )

    XAML

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

    Codebehind

    filterAllText.DataContext = LogSession.test.MyCoynt;
    

    C#

    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));
            }
    }
    
  • blindmeis
    blindmeis over 11 years
  • default
    default over 11 years
    oh, I didn't see that he set the DataContext to the actual property. Cool trick :)