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 DependencyPropert开发者_如何学Cy 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
You can either:
- look at the sender in the event handler
- attach both controls to different event handlers
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.
var descriptor = DependencyPropertyDescriptor.FromProperty(YourType.StartTimeProperty , tpeof(YourType));
descriptor.AddValueChanged(obj1, OnStartTimeChanged1);
descriptor.AddValueChanged(obj2, OnStartTimeChanged2);
精彩评论