C# WPF TextBlock 文本绑定
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14624373/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 12:28:06 来源:igfitidea点击:
WPF TextBlock text Binding
提问by persianLife
the TextBlock binding does not work and I cant figure why...
TextBlock 绑定不起作用,我不知道为什么......
(This Code Works but the TextBlock does not get Updated )
(此代码有效,但 TextBlock 未更新)
XAML
XAML
<TextBlock x:Name="filterAllText"
Text="{Binding UpdateSourceTrigger=PropertyChanged}" />
Codebehind
代码隐藏
filterAllText.DataContext = LogSession.test.MyCoynt;
C#
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));
}
}
采纳答案by Goanne
Try this:
尝试这个:
<TextBlock x:Name="filterAllText"
Text="{Binding UpdateSourceTrigger=PropertyChanged, Path=MyCoynt}" />
And set your DataContext
like:
并设置您的DataContext
喜好:
filterAllText.DataContext = LogSession.test;
回答by blindmeis
<TextBlock x:Name="filterAllText" Text="{Binding Path=., UpdateSourceTrigger=PropertyChanged}" />
this should work but its not the usual way
这应该可行,但不是通常的方式
EDIT: the better way is the anwser from Goanne
编辑:更好的方法是来自 Goanne 的 anwser