在 WPF 中,我希望能够对默认情况下如何应用我的绑定(bind)进行模板化。
例如,我想写:
Text="{Binding Path=PedigreeName}"
但就好像我输入了:
Text="{Binding Path=PedigreeName, Mode=TwoWay, UpdateSourceTrigger=LostFocus, NotifyOnValidationError=True, ValidatesOnDataErrors=True, ValidatesOnExceptions=True}"
任何的想法 ?
谢谢,
最佳答案
使用 DependencyProperty.Register 的重载之一采用 PropertyMetadata。传递 FrameworkPropertyMetadata 的实例并设置其属性。
public class Dog {
public static readonly DependencyProperty PedigreeNameProperty =
DependencyProperty.Register("PedigreeName", typeof(string), typeof(Dog),
new FrameworkPropertyMetadata() {
BindsTwoWayByDefault = true,
DefaultUpdateSourceTrigger = UpdateSourceTrigger.LostFocus
}
);
我没有立即看到设置 NotifyOnValidationError、ValidatesOnDataErrors 或 ValidatesOnExceptions 的默认值的方法,但我没有充分使用它来确定要查找的内容;他们可能在那里。
关于WPF : Define binding's default,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1094509/