如何确定 DependencyProperty 的更改内容
我有两个对象绑定到相同的 dependencyProperty(在 Silverlight 中)。 有没有办法确定这两个对象中哪一个改变了属性? 我想根据这些信息采取不同的行动。
不幸的是,我无法附加两个不同的事件处理程序(因为它是一个 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
}
在此示例中,我将从不同的对象设置 StartTime 属性,并且我想知道其中哪个对象更改了该属性。
谢谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以:
You can either:
我最终捕获了控件上的 mouseDown 事件,因此我知道 dependencyProperty 的值已由用户界面更改。这不是最干净的解决方案,但它有效。
非常感谢您的所有建议。
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.