wpf - 依赖属性的数据绑定(bind)丢失

标签 wpf data-binding dependency-properties

我有一个自定义控件,在其中创建了一个名为 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/

相关文章:

c# - 在 .Net 4.0 中从 .Net 4.5 绑定(bind)的延迟属性

javascript - Knockout.js 将 2D 表与 rowspan 绑定(bind)

c# - 如何创建一个只读 bool 依赖属性,该属性在其他两个依赖属性之间返回 `And` 操作

wpf - 如果我们不能绑定(bind) MouseBinding 的 Command,我们应该怎么做?

wpf - INotifyPropertyChanged 与 ViewModel 中的 DependencyProperty

c# - 如何加速 .NET 客户端应用程序(Windows 窗体或 WPF)的初始化?

c# - C#WPF INotifyChanged值相同时不调用

c# - 使用 MVVM 模式验证 ViewModel 中绑定(bind)的 ObservableCollection

wpf - 当绑定(bind)值发生变化时,启动动画的最佳方式是什么?

wpf - 数据绑定(bind)在 xaml 中不起作用