2011-11-04 5 views
1

TextBox에서 파생 된 MyTextBox가 있습니다. 바인딩 옵션을 설정하고 싶습니다. ValidatesOnDataErrors = MyTextBox의 TextProperty 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. 

내가 뭔가를 놓치고 있습니까?

답변

3

당신이 필요로하는 것은 특수 문자 입력이 아닌 특수 바인딩이라고 생각합니다. 이 사용되면 WPF에서 Set ValidatesOnDataErrors for all bindings programmatically

당신이 바인딩을 변경할 수 없습니다 :

여기를 보라.

코드 작업을 수행하려면 먼저 바인딩을 지운 다음 ValidatesOnDataErrors를 true로 설정하여 새 코드를 추가해야하지만 그 코드는 지저분한 방식으로 들립니다.

관련 문제