INotifyPropertyChanged的用户控件中 [英] INotifyPropertyChanged in UserControl

查看:205
本文介绍了INotifyPropertyChanged的用户控件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个从TextBox控件继承的自定义控件。
我想实现我的自定义控件 INotifyPropertyChanged的接口。

 公共类CustomTextBox:文本框,INotifyPropertyChanged的
{
公共事件PropertyChangedEventHandler的PropertyChanged;
保护无效NotifyPropertyChanged(字符串信息)
{
如果(的PropertyChanged!= NULL)
的PropertyChanged(这一点,新PropertyChangedEventArgs(信息));
}
}



我的问题是,当我尝试提出一个PropertyChanged事件PropertyChanged事件处理程序始终为空。



任何人都可以帮我吗?


解决方案

PropertyChanged事件处理程序alwas空。




这将永远是真,直到事情订阅在的PropertyChanged 事件。



通常,如果你正在做一个自定义的控制,但是,你不会用 INotifyPropertyChanged的。在这种情况下,你会成为一个自定义依赖属性代替。通常情况下,依赖对象(即:对照)将所有使用依赖属性,INPC使用由成为 DataContext的这些对象的类。这使得绑定系统的正常工作。


I have a custom control which is inherited from TextBox control. I would like to implement the INotifyPropertyChanged interface in my custom control.

public class CustomTextBox : TextBox, INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    protected void NotifyPropertyChanged(string info)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(info));
    }
}

My problem is when I try to raised a PropertyChanged event the PropertyChanged event handler is always null.

Anybody can help me?

解决方案

the PropertyChanged event handler is alwas null.

This will always be true until something subscribes to the PropertyChanged event.

Typically, if you're making a custom control, however, you wouldn't use INotifyPropertyChanged. In this scenario, you'd make a Custom Dependency Property instead. Normally, the dependency objects (ie: controls) will all use Dependency Properties, and INPC is used by the classes which become the DataContext of these objects. This allows the binding system to work properly.

这篇关于INotifyPropertyChanged的用户控件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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