6

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

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

XAML

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

代码隐藏

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));
        }
}
4

2 回答 2

10

试试这个:

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

并设置你的DataContext喜欢:

filterAllText.DataContext = LogSession.test;
于 2013-01-31T11:20:28.220 回答
0
<TextBlock x:Name="filterAllText" Text="{Binding Path=., UpdateSourceTrigger=PropertyChanged}" />

这应该可以,但不是通常的方式

编辑:更好的方法是来自 Goanne

于 2013-01-31T11:20:51.157 回答