c# – WPF TextBlock文本绑定

前端之家收集整理的这篇文章主要介绍了c# – WPF TextBlock文本绑定前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
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;
原文链接:https://www.f2er.com/csharp/91607.html

猜你在找的C#相关文章