我有从 TextBox 派生的 MyTextBox。我想在 MyTextBox 中设置 TextProperty 的 Binding Option ValidatesOnDataErrors = True,这样每当我使用这个控件时,ValidatesOnDataErrors 就会被初始化为 True。
这是我的代码:
public class MyTextBox:MyBaseTextBox
{
public MyTextBox()
{
MaxLength = 45;
}
protected override void OnPropertyChanged(System.Windows.DependencyPropertyChangedEventArgs e)
{
base.OnPropertyChanged(e);
if (e.Property == TextProperty)
{
Binding b = BindingOperations.GetBinding(this, TextProperty);
if (b != null)
{
b.ValidatesOnDataErrors = true;
}
}
}
}
我总是得到例外:
An unhandled exception of type 'System.InvalidOperationException' occurred in PresentationFramework.dll
Additional information: Binding cannot be changed after it has been used.
我错过了什么吗?