WPF TextBlock文本绑定

per*_*ife 6 c# data-binding wpf user-controls

TextBlock绑定不起作用,我不知道为什么......

(此代码有效,但TextBlock未更新)

XAML

<TextBlock x:Name="filterAllText"
 Text="{Binding UpdateSourceTrigger=PropertyChanged}" />
Run Code Online (Sandbox Code Playgroud)

代码隐藏

filterAllText.DataContext = LogSession.test.MyCoynt;
Run Code Online (Sandbox Code Playgroud)

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));
        }
}
Run Code Online (Sandbox Code Playgroud)

Goa*_*nne 9

试试这个:

<TextBlock x:Name="filterAllText" 
    Text="{Binding UpdateSourceTrigger=PropertyChanged, Path=MyCoynt}" />
Run Code Online (Sandbox Code Playgroud)

并设置你的DataContext喜好:

filterAllText.DataContext = LogSession.test;
Run Code Online (Sandbox Code Playgroud)