2013-09-06 2 views
1

컨트롤이있는 UserControl 창이 있습니다.
UserControl에 대한 Enabled 속성을 추가하여 선택한 컨트롤의 상태를 IsReadOnly 상태로 제어하고 싶습니다.많은 컨트롤에 전파하는 IsReadonly 변수

어떻게하면됩니까?
는 부모에게 UserControl 바인드 IsReadOnly 속성에 각 자식 컨트롤에 대해

답변

2

:-) 감사 :

<TextBox IsReadOnly="{Binding Enabled, RelativeSource={RelativeSource AncestorType={x:Type typeOfUserControl}}}"> 

와 당신 UserControl에 대한 EnabledDependancy property을 정의합니다. 당신은 특정 유형을 수행하는 연산자로 사용할 수 있습니다 MSDN

에서 :

[ValueConversion(typeof(bool), typeof(bool))] 
    public class InverseBooleanConverter: IValueConverter 
    { 
     public object Convert(object value, Type targetType, object parameter, 
      System.Globalization.CultureInfo culture) 
     { 
      if (targetType != typeof(bool)) 
       throw new InvalidOperationException("The target must be a boolean"); 

      return !(bool)value; 
     } 

     public object ConvertBack(object value, Type targetType, object parameter, 
      System.Globalization.CultureInfo culture) 
     { 
      throw new NotSupportedException(); 
     } 
    } 

UPD : 가능 -에 - 읽기 전용 로직

당신은 또한 변환 부울 역 변환기를 사용한다 호환되는 참조 유형 또는 null 입력 가능 유형 간의 변환

그래서 :

public static readonly DependencyProperty EnabledDependencyProperty = 
    DependencyProperty.Register("Enabled", typeof(bool), 
    typeof(UserControlType), new FrameworkPropertyMetadata(true)); 

public bool Enabled 
{ 
    get { return (bool)GetValue(EnabledDependencyProperty); } 
    set { SetValue(EnabledDependencyProperty, value); } 
} 
+0

어떻게 여기 내'UserControl'에 대한'Enabled'을 정의합니까? 시도'get {return base.GetValue (EnabledProperty) bool; }'하지만 오류가 발생합니다 (bool은 nullable이 아닙니다). –

+0

@ JacekWojcik : UPD를 참조하십시오. –

관련 문제