我有一个自定义控件,在其中创建了一个名为 TheObject
的自定义 DependencyProperty
,它可以包含通用对象。
<comp:MyControl TheObject="{Binding Country}" />
有时,我需要在内部设置TheObject
(通过代码,在控件内部)。
我做了这样的事情:
this.TheObject = new Country();
但我意识到它导致 DataBinding
丢失,并且控件变得不响应数据更改。
我真正想要的是这个新对象仍然附加到属性的现有 DataBinding
。
最佳答案
使用SetCurrentValue
:
This method is used by a component that programmatically sets the value of one of its own properties without disabling an application's declared use of the property. The SetCurrentValue method changes the effective value of the property, but existing triggers, data bindings, and styles will continue to work.
this.SetCurrentValue(TheObjectProperty, new Country());
关于wpf - 依赖属性的数据绑定(bind)丢失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9266579/