我正在研究一个附加属性,需要告诉附加属性两件事。绑定和属性名称。在附加的属性内部,存在一种逻辑,其中该逻辑使用类名和属性名来确定控件的行为(而不仅仅是文本框),如更改文本的颜色或更改可见性等。
使用此代码,我可以确定MyAttached中的类名称和属性名称。但是问题是,如果SomeText的值为null,则不会触发OnBindingChanged。
<Textbox Text="{Binding SomeText}"
MyAttached.Binding="{Binding SomeText}" />
所以我得到了这样的结果,这个对我有用:
<Textbox Text="{Binding SomeText}"
MyAttached.Path="SomeText"
MyAttached.Binding="{Binding}" />
大多数情况下,MyAttached.Binding将是=“ {Binding}”。有什么办法可以消除这条线吗?还是确定MyAttached代码中的绑定?
我的目标是这样的:
<Textbox Text="{Binding SomeText}"
MyAttached.Path="SomeText" />
并在此处确定绑定源:
private static async void OnPathChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
string propertyName = e.NewValue.ToString();
// Is it possible to get this without using another attached property?
// Can I get it from the dependency object?
// object bindingSource = ???;
ActivateBehavior(bindingSource, propertyName);
}