加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – WPF TextBlock文本绑定

发布时间:2020-12-15 04:25:23 所属栏目:百科 来源:网络整理
导读:TextBlock绑定不起作用,我不知道为什么…… (此代码有效,但TextBlock未更新) XAML TextBlock x:Name="filterAllText" Text="{Binding UpdateSourceTrigger=PropertyChanged}" / 代码隐藏 filterAllText.DataContext = LogSession.test.MyCoynt; C# public cl
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;

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读