One option is to have your object implement the very standard INotifyPropertyChanged interface. There's no rule which states that every property on an object needs to raise the PropertyChanged event, so just raise it for the one property in question. Something like this:
public class MyObject : INotifyPropertyChanged
{
private string _myProperty;
public string MyProperty
{
get { return _myProperty; }
set
{
_myProperty = value;
NotifyPropertyChanged();
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
If your .NET version doesn't support CallerMemberName then you can remove that and supply the property name manually:
NotifyPropertyChanged("MyProperty");
At this point any calling code can subscribe to the public PropertyChanged event on any instance of this object. This interface is particularly handy because it's commonly used in lots of UI technologies for updating data-bound UI elements. All the interface enforces, really, is a standard name for the event. If you'd prefer, you can forgo the interface and just create a custom public event. And just invoke that event any time your object logically needs to. The structure of exposing an event and invoking it would be the same.
INotifyPropertyChangedas a standard interface and invoke thePropertyChangedevent in the property's setter.public int SelectedIndex { get; private set; //set { selectedIndex = value; } }Now, how do I add INotifyPropertyChanged for only this property so that it triggers an event when the value is changed?