Observe the change in property

I have 2 class properties (WPF control): HorizontalOffset and VerticalOffset (both public Double 's). I would like to call a method when these properties change. How can i do this? I know one way - but I'm sure this is not the right way (using DispatcherTimer with very short tick intervals to control the property).

EDIT MORE CONTEXT:

These properties apply to the telerik schedule control.

+8
c # properties
source share
2 answers

Use an implementation of the INotifyPropertyChanged control.

If the control is called myScheduleView :

 //subscribe to the event (usually added via the designer, in fairness) myScheduleView.PropertyChanged += new PropertyChangedEventHandler( myScheduleView_PropertyChanged); private void myScheduleView_PropertyChanged(Object sender, PropertyChangedEventArgs e) { if(e.PropertyName == "HorizontalOffset" || e.PropertyName == "VerticalOffset") { //TODO: something } } 
+17
source share

I know one way ... DispatcherTimer

Wow, avoid this: INotifyPropertyChange interface is your friend. See msdn for samples.

Basically, you onPropertyChanged event (usually called onPropertyChanged ) on the Setter your properties, and subscribers process it.

An example implementation from msdn goes:

 // This is a simple customer class that // implements the IPropertyChange interface. public class DemoCustomer : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; private void NotifyPropertyChanged(String info) { if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(info)); } public string CustomerName { //getter set { if (value != this.customerNameValue) { this.customerNameValue = value; NotifyPropertyChanged("CustomerName"); } } } } 
+5
source share

All Articles