TextBlock绑定(bind)不起作用,我无法弄清楚为什么...
(此代码有效,但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));
}
}
最佳答案
试试这个:
<TextBlock x:Name="filterAllText"
Text="{Binding UpdateSourceTrigger=PropertyChanged, Path=MyCoynt}" />
并将您的
DataContext
设置为:filterAllText.DataContext = LogSession.test;
关于c# - WPF TextBlock文本绑定(bind),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14624373/