2010-04-08 4 views
0

내부 DependencyProperties가 변경된 경우 DependencyObject의 bindinigs에 알릴 수있는 방법이 있습니까? 내가 COLOR1, COLOR2 및 UseBothColors에 대한 값을 설정 세 별도의 양방향 바인딩이DependencyProperites가 변경된 경우 DependencyObject에 대한 호출 바인딩

public class BackgroundDef : DependencyObject 
    { 
     public static readonly DependencyProperty Color1Property = 
      DependencyProperty.Register("Color1", typeof(Color), 
       typeof(BackgroundDef), new UIPropertyMetadata(Colors.White)); 

     public static readonly DependencyProperty UseBothColorsProperty = 
      DependencyProperty.Register("UseBothColors", typeof(bool), 
       typeof(BackgroundDef), new UIPropertyMetadata(false)); 

     public static readonly DependencyProperty Color2Property = 
      DependencyProperty.Register("Color2", typeof(Color), 
       typeof(BackgroundDef), new UIPropertyMetadata(Colors.White)); 

     public Color Color1 
     { 
      set { SetValue(Color1Property, value); } 
      get { return (Color)GetValue(Color1Property); } 
     } 

     public bool UseBothColors 
     { 
      set { SetValue(UseBothColorsProperty, value); } 
      get { return (bool)GetValue(UseBothColorsProperty); } 
     } 

     public Color Color2 
     { 
      set { SetValue(Color2Property, value); } 
      get { return (Color)GetValue(Color2Property); } 
     } 
    } 

하는 경우 :

예를 들어,이 클래스가 있습니다. 그러나 BackgroundDef 인스턴스에 대한 바인딩도 있습니다.이 인스턴스는 브러시를 만들고 단추의 배경을 그려야합니다 (단색 또는 두 가지 그라디언트 색상). 내 문제는 DependencyProperties에 대한 양방향 바인딩이 속성을 업데이트하지만 클래스 인스턴스에 대한 바인딩이 호출되지 않는다는 것입니다. 분명히 전체 개체가 변경되지 않기 때문입니다. 어떤 생각 DependencyProperties 변경 될 때 어떻게 DependencyObject에 대한 바인딩을 호출 할 수 있습니까?

답변

1

다음과 같이 할 수 있습니다.

다중 바인딩을 사용하여 클래스에 바인딩하는 대신 세 개의 값을 모두 바인딩합니다. 값 중 하나가 변경 될 때마다 바인딩이 다시 평가됩니다.

또는 (이 내가 사용하는 것이 기술입니다) : 클래스 BackgroundDef가 다른 클래스의 속성 인 경우

, 당신은 BackgroundDef의 속성 중 하나를 변경할 때마다 해당 클래스에 NotifyPropertyChanged 이벤트를 발생시킬 수있다. 물론 이는 부모 클래스 인 BackgroundDef에 재산을 갖고 자녀가 변경 될 때마다 부모에게 알리는 것을 의미합니다.

관련 문제