13

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

EDIT FOR MORE CONTEXT:

These properties belong to a telerik scheduleview control.

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
  • 1
    Use events? http://msdn.microsoft.com/en-us/library/awbftdfh.aspx – Andras Zoltan Feb 29 '12 at 15:34
  • I know how to subscribe to existing events - but I have no experience with creating my own events ready for subscription - is this possible? Is this the way you would say is most efficient for what I want to achieve here? –  Feb 29 '12 at 15:36
  • 2
    Well, given that these are two properties on a type you don't own; you need to see what mechanism, if any, Telerik have exposed on the control for monitoring those properties. Given it's WPF, I would have thought it's `INotifyPropertyChanged`. In this case you're not exposing your own event source, you need to hope that one already exists on that control – Andras Zoltan Feb 29 '12 at 15:38
  • I'll have a look into that now then. But assuming there isn't already a way of monitoring these properties changing - is there no other way to observe properties?? –  Feb 29 '12 at 15:40
  • 1
    Not really, no. Certainly using a timer is not the way forward. Casting an eye over the (slightly sticky, it has to be said) Telerik API docs, the control appears to implement INotifyPropertyChanged; therefore you subscribe to it's `PropertyChanged` event and do something when the args.PropertyName is either `"HorizontalOffset"` or `"VerticalOffset"` – Andras Zoltan Feb 29 '12 at 15:46

2 Answers2

25

Leverage the INotifyPropertyChanged interface implementation of the 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
  }
}
Andras Zoltan
  • 41,961
  • 13
  • 104
  • 160
6

I know of one way ... DispatcherTimer

Wow avoid that :) INotifyPropertyChange interface is your friend. See the msdn for samples.

You basically fire an event(usually called onPropertyChanged) on the Setter of your properties and the subscribers handle it.

an example implementation from the 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");
            }
        }
    }
}
Orkun
  • 6,998
  • 8
  • 56
  • 103