[英]Notify when the binding of Dependency Property is changed
我需要調試給定依賴項屬性的綁定設置。 首先,我使用以下代碼將綁定設置為給定源實例的依賴項屬性:
var binding = new Binding(path);
binding.Source = source;
binding.Mode = twoWay ? BindingMode.TwoWay : BindingMode.OneWay;
binding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
binding.Converter = valueConverter;
var bindingResult = BindingOperations.SetBinding(this, ModelValueProperty, binding);
var bindingExpression = BindingOperations.GetBindingExpression(this, ModelValueProperty);
bindingExpression不為null,綁定狀態為Active。 經過一些視圖操作后,當我嘗試獲取bindingExpression時,它將為null。 如何在給定的依賴項屬性上捕獲綁定替換或更改?
編輯:以另一種方式,當bindExpression的狀態從Active更改為Detached時,我想知道如何獲得通知
您需要設置此屬性:
binding.NotifyOnSourceUpdated = true
並注冊到您要綁定的控件的SourceUpdated
事件(在您的情況下就是this
):
this.SourceUpdated += (s, args) =>
{
// Catch changes there
};
或者 ,你可以捕捉直接對所做的更改DependencyProperty
用DependencyPropertyDescriptor
:
var descriptor = DependencyPropertyDescriptor.FromProperty(ModelValueProperty, typeof(YourType));
descriptor.AddValueChanged(this, OnValueChanged);
private void OnValueChanged(object sender, EventArgs e)
{
//...
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.