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 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 .
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With