WPF TextBlock的文本装订 [英] WPF TextBlock text Binding

查看:219
本文介绍了WPF TextBlock的文本装订的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

TextBlock的绑定不工作,我不能弄清楚为什么...



(此代码的工作,但文本块没有更新)



XAML



<预类=郎咸平的XML prettyprint-覆盖> < TextBlock的X:名称=filterAllText
文本={绑定UpdateSourceTrigger = PropertyChanged的}/>



代码隐藏



  filterAllText.DataContext = LogSession.test.MyCoynt; 



C#



 公共类测试:INotifyPropertyChanged的{
公众诠释myCoynt;

公众诠释MyCoynt {
{返回myCoynt; }
集合{
myCoynt =价值;
NotifyPropertyChanged();
}
}

公共事件PropertyChangedEventHandler的PropertyChanged;

受保护的虚拟无效NotifyPropertyChanged(
[CallerMemberName]字符串参数propertyName =){
PropertyChangedEventHandler处理器=的PropertyChanged;
如果(处理!= NULL){
处理器(这一点,新PropertyChangedEventArgs(propertyName的));
}
}


解决方案

尝试这样的:



<预类=郎咸平的XML prettyprint-覆盖> < TextBlock的X:名称=filterAllText
文本= {结合UpdateSourceTrigger = PropertyChanged的,路径= MyCoynt}/>

和设置您的的DataContext ,如:

  filterAllText.DataContext = LogSession.test; 


the TextBlock binding does not work and I cant figure why...

(This Code Works but the TextBlock does not get Updated )

XAML

<TextBlock x:Name="filterAllText"
 Text="{Binding UpdateSourceTrigger=PropertyChanged}" />

Codebehind

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));
        }
}

解决方案

Try this:

<TextBlock x:Name="filterAllText" 
    Text="{Binding UpdateSourceTrigger=PropertyChanged, Path=MyCoynt}" />

And set your DataContext like:

filterAllText.DataContext = LogSession.test;

这篇关于WPF TextBlock的文本装订的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆