Adam Bilinski
Adam Bilinski

Reputation: 1198

How to determine what changed a DependencyProperty

I've got two objects bound to the same dependencyProperty (in Silverlight). Is there a way to determine which of these two objects changed the property? I want to take different actions based on that information.

Unfortunately, I cannot attach two different eventHandlers (because it's a dependencyProperty)

   public int StartTime
    {
        get { return (int)GetValue(StartTimeProperty); }
        set { SetValue(StartTimeProperty, value); }
    }
    public static readonly DependencyProperty StartTimeProperty =
        DependencyProperty.Register("StartTime", typeof(int), typeof(Step),
        new PropertyMetadata(-1, new PropertyChangedCallback(OnStartTimeChanged)));

    private static void OnStartTimeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        ((Step)d).OnStartTimeChanged(e);
    }

    protected virtual void OnStartTimeChanged(DependencyPropertyChangedEventArgs e)
    {
        //if set from obj1 -> do something
        //if set from obj2 -> do something else
    }

In this example I would be setting StartTime property from different objects and I want to know which of these object changed the property.

Thanks

Upvotes: 2

Views: 206

Answers (3)

Adam Bilinski
Adam Bilinski

Reputation: 1198

I have ended up catching a mouseDown event on the control so I knew the value of a dependencyProperty was changed by the user interface. It is not the cleanest solution but it worked.

Many thanks for all your suggestions.

Upvotes: 2

treehouse
treehouse

Reputation: 2531

var descriptor = DependencyPropertyDescriptor.FromProperty(YourType.StartTimeProperty , tpeof(YourType));
descriptor.AddValueChanged(obj1, OnStartTimeChanged1);
descriptor.AddValueChanged(obj2, OnStartTimeChanged2);

Upvotes: -1

codymanix
codymanix

Reputation: 29468

You can either:

  • look at the sender in the event handler
  • attach both controls to different event handlers

Upvotes: 3

Related Questions