Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

INotifyPropertyChanged in UserControl

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?

like image 989
user295541 Avatar asked Sep 17 '12 19:09

user295541


People also ask

What is the purpose of INotifyPropertyChanged?

The INotifyPropertyChanged interface is used to notify clients, typically binding clients, that a property value has changed. For example, consider a Person object with a property called FirstName .

How is INotifyPropertyChanged implemented?

To implement INotifyPropertyChanged you need to declare the PropertyChanged event and create the OnPropertyChanged method. Then for each property you want change notifications for, you call OnPropertyChanged whenever the property is updated.

What is INotifyPropertyChanged xamarin?

The PropertyChanged event notifies the UI that a property in the binding source (usually the ViewModel) has changed. It allows the UI to update accordingly. The interface exists for WPF, Silverlight, UWP, Uno Platform, and Xamarin.


1 Answers

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.

like image 114
Reed Copsey Avatar answered Sep 23 '22 11:09

Reed Copsey