如何传播 DependencyProperty 中的 PropertyChanged 更改
我有一个实现 INotifyPropertyChanged 的类。此类的实例在窗口中声明为 DependencyProperty,例如,
public IMyClass MyClass
{
get { return (IMyClass)GetValue(MyClassProperty); }
set { SetValue(MyClassProperty, value); }
}
public static readonly DependencyProperty MyClassProperty=
DependencyProperty.Register("MyClass", typeof(IMyClass), typeof(MainWindow), new UIPropertyMetadata(null));
在 XAML 中,我有一个使用绑定到此类的元素,
Text="{Binding MyClass, Converter={StaticResource someConverter}}
每当我更改 MyClass 中的属性时,我希望触发 someConverter。然而,只有当我完全替换 MyClass 时才会发生这种情况。有没有办法将 DependencyProperty 更新与我的 MyClass PropertyChanged 联系起来?
更新。 本着 AresAvatar 解决方案的精神,以下是我们目前所拥有的内容。剩下的问题是如何调用 InvalidateProperty (没有 MyClass 跟踪它......)
public IMyClass MyClass
{
get { return (IMyClass)GetValue(MyClassProperty); }
set { SetValue(MyClassProperty, value); }
}
public static readonly DependencyProperty MyClassProperty =
DependencyProperty.Register("MyClass", typeof(IMyClass), typeof(MainWindow),
new UIPropertyMetadata(null, new PropertyChangedCallback(OnMyClassChanged)));
private static void OnMyClassChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (e.OldValue != null)
{
((IMyClass)e.OldValue).PropertyChanged -= ((MainWindow)d).MyClass_PropertyChanged;
}
if (e.NewValue != null)
{
((IMyClass)e.NewValue).PropertyChanged += ((MainWindow)d).MyClass_PropertyChanged;
}
}
private void MyClass_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
this.InvalidateProperty(MyClassProperty); <----- still does not refresh binding, but called.
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
转换器不应该做比简单转换更多的工作,您的问题听起来像是转换器使用对象的许多属性来创建一些组合值。使用
MultiBinding
而是挂钩到您需要的对象上的所有不同属性,这样MultiValueConverter
将触发。此外,由于您似乎创建了文本,因此您可能根本不需要使用任何转换器即可摆脱
StringFormat
可能就足够了。Converters should not do more work than simple conversions, your question sounds like the converter uses a lot of properties of the object to create some combined value. Use a
MultiBinding
instead which hooks into all the different properties on the object you need, that way theMultiValueConverter
on thatMultiBinding
will fire if any of those properties change.Further, since you seem to create text you might be able to get away without using any converter at all as the
StringFormat
might be enough.我发现的唯一技术是在策略性放置的事件处理程序(例如 LostFocus)中调用绑定的 UpdateSource 方法。
如果您不关心闲聊或者您的控件不接受输入焦点,您可以在 mycontrol_PropertyChanged 事件或类似事件中执行此操作。但是,对每个属性更改或每次击键强制执行转换周期可能会干扰验证。
The only technique I've found is to call the binding's UpdateSource method in a strategically placed event handler, such as LostFocus.
If you don't care about chattiness or if your control doesn't take input focus, you could do this in mycontrol_PropertyChanged event or similar. However, forcing a conversion cycle on every property change or every keystroke may interfere with validation.
在 MyClass 中,实现 NotifyPropertyChanged 事件。然后将属性更改回调添加到 MyClass DependencyProperty。在 DP 的属性更改回调中,将新的 MyClass NotifyPropertyChanged 事件挂钩到第二个回调函数(并使用 -= 运算符取消挂钩先前的值(如果有))。在第二个回调函数中,调用 DependencyObject.InvalidateProperty 以便更新绑定。
编辑:您可能需要通过以下方式触发绑定更新:
In MyClass, implement a NotifyPropertyChanged event. Then add a property changed callback to your MyClass DependencyProperty. In the DP's property changed callback, hook your new MyClass NotifyPropertyChanged event to a second callback function (and unhook the previous value, if any, with a -= operator). In the second callback function, call DependencyObject.InvalidateProperty so that the binding gets updated.
Edit: you may need to trigger a binding update with: