c# - WPF TextBlock 文本绑定(bind)

标签 c# wpf data-binding user-controls

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/

相关文章:

c# - 将对象集合绑定(bind)到 ListBox

c# - 通知 WPF DataGrid 更改

c# - 如何使用 C# 在文本框中查找插入符位置?

c# - 使用 WCF 的 DDD 项目结构

c# - 使用语言代码 (en) 还是完整的文化代码(例如 en-AU)?

c# - C#中如何将数据表绑定(bind)到datagridview

c# - 使用 XPath 绑定(bind)表达式时,是否可以取回 InnerXml 而不是 InnerText?

c# - MVC 5 : Should I inherit my User from IdentityUser class?

c# - 如何使用亚洲格式格式化钱

WPF Listbox - 空列表显示消息