[英]WPF TextBlock text Binding
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));
}
}
尝试这个:
<TextBlock x:Name="filterAllText"
Text="{Binding UpdateSourceTrigger=PropertyChanged, Path=MyCoynt}" />
并将您的DataContext
设置为:
filterAllText.DataContext = LogSession.test;
<TextBlock x:Name="filterAllText" Text="{Binding Path=., UpdateSourceTrigger=PropertyChanged}" />
这应该工作,但它不是通常的方式
编辑:更好的方式是来自Goanne的anwser
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.