C# WPF如何侦听BindingBase对象?

C# WPF如何侦听BindingBase对象?,c#,wpf,binding,datatrigger,C#,Wpf,Binding,Datatrigger,我想创建一个特殊的DataTrigger来继承TriggerBase。与DataTrigger类似,在MyDataTrigger类中定义了类型为BindingBase的属性 我如何听它来追踪它的变化 public class MyDataTrigger : TriggerBase<FrameworkElement> { ... /// <summary> /// [Wrapper property for BindingProperty]

我想创建一个特殊的
DataTrigger
来继承
TriggerBase
。与
DataTrigger
类似,在
MyDataTrigger
类中定义了类型为
BindingBase
的属性

我如何听它来追踪它的变化

public class MyDataTrigger : TriggerBase<FrameworkElement>
{
    ...

    /// <summary>
    /// [Wrapper property for BindingProperty]
    /// <para>
    /// Gets or sets the binding that produces the property value of the data object.
    /// </para>
    /// </summary>
    public BindingBase Binding
    {
        get { return (BindingBase)GetValue(BindingProperty); }
        set { SetValue(BindingProperty, value); }
    }

    public static readonly DependencyProperty BindingProperty =
        DependencyProperty.Register("Binding",
                                    typeof(BindingBase),
                                    typeof(MyDataTrigger),
                                    new FrameworkPropertyMetadata(null));

}
其中,
DPListener\u ValueChanged
是一个
EventHandler
委托。在这里,组件参数值是this.AssociatedObject

好的,找到了

考虑到这一点,绑定不是DP。因此,我试图找到与DP相关的绑定:

Type type = Binding.GetType();
PropertyPath propertyPath = (PropertyPath)(type.GetProperty("Path").GetValue(Binding));
string propertyName = propertyPath.Path;
完整代码:

public class MyDataTrigger : TriggerBase<FrameworkElement>
{
    ...

    public BindingBase Binding
    {
        get;
        set;
    }

    protected override void OnAttached()
    {
        base.OnAttached();

        if (Binding != null && this.AssociatedObject.DataContext != null)
            //
            // Adding a property changed listener..
            //
            Type type = Binding.GetType();
            PropertyPath propertyPath = (PropertyPath)(type.GetProperty("Path").GetValue(Binding));
            string propertyName = propertyPath.Path;

            TypeDescriptor.GetProperties(this.AssociatedObject.DataContext).Find(propertyName, false).AddValueChanged(this.AssociatedObject.DataContext, PropertyListener_ValueChanged);
    }

    private void PropertyListener_ValueChanged(object sender, EventArgs e)
    {
        // Do some stuff here..
    }
}
公共类MyDataTrigger:TriggerBase
{
...
公共绑定基绑定
{
收到
设置
}
受保护的覆盖无效附加()
{
base.onatached();
if(Binding!=null&&this.AssociatedObject.DataContext!=null)
//
//添加属性已更改的侦听器。。
//
Type=Binding.GetType();
PropertyPath PropertyPath=(PropertyPath)(type.GetProperty(“路径”).GetValue(绑定));
字符串propertyName=propertyPath.Path;
TypeDescriptor.GetProperties(this.AssociatedObject.DataContext).Find(propertyName,false).AddValueChanged(this.AssociatedObject.DataContext,PropertyListener\u ValueChanged);
}
私有void PropertyListener\u值已更改(对象发送方,事件参数e)
{
//在这里做些事情。。
}
}

关联的DP总是
DataContext
,这是真的吗?这不能被标记为一般答案。这里只考虑
BindingBase
Path
属性。(例如,不是
XPath
)。有什么建议吗?使用。。。var propertyName=((Binding)this.Binding).Path.Path;
public class MyDataTrigger : TriggerBase<FrameworkElement>
{
    ...

    public BindingBase Binding
    {
        get;
        set;
    }

    protected override void OnAttached()
    {
        base.OnAttached();

        if (Binding != null && this.AssociatedObject.DataContext != null)
            //
            // Adding a property changed listener..
            //
            Type type = Binding.GetType();
            PropertyPath propertyPath = (PropertyPath)(type.GetProperty("Path").GetValue(Binding));
            string propertyName = propertyPath.Path;

            TypeDescriptor.GetProperties(this.AssociatedObject.DataContext).Find(propertyName, false).AddValueChanged(this.AssociatedObject.DataContext, PropertyListener_ValueChanged);
    }

    private void PropertyListener_ValueChanged(object sender, EventArgs e)
    {
        // Do some stuff here..
    }
}