1
我有從TextBox派生的MyTextBox。我想在MyTextBox中設置Binding Option ValidatesOnDataErrors = True的TextProperty,這樣每當我使用這個控件時,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.
我缺少的東西?