2013-08-08 2 views
0

두 종속성 속성, ValueMinVal이 있습니다.
"값"의 기본값은 "MinVal"에 달려 있습니다.
"MinVal"은 xaml로 한 번만 설정합니다.
어떻게하면됩니까?종속성 속성 기본값은 다른 종속성 속성에 따라 다릅니다.

public int Value 
    { 
     get { return (int)GetValue(ValueProperty); } 
     set { SetValue(ValueProperty, value); } 
    } 

    // Using a DependencyProperty as the backing store for Value. This enables animation, styling, binding, etc... 
    public static readonly DependencyProperty ValueProperty = 
     DependencyProperty.Register("Value", typeof(int), typeof(NumericHexUpDown), new UIPropertyMetadata(0, ValueChanged)); 

    private static void ValueChanged(object sender, DependencyPropertyChangedEventArgs e) 
    { 

    } 


    public int MinVal 
    { 
     get { return (int)GetValue(MinValProperty); } 
     set { SetValue(MinValProperty, value); } 
    } 

    // Using a DependencyProperty as the backing store for MinVal. This enables animation, styling, binding, etc... 
    public static readonly DependencyProperty MinValProperty = 
     DependencyProperty.Register("MinVal", typeof(int), typeof(NumericHexUpDown), new UIPropertyMetadata(0, MinValueChanged)); 
    private static void MinValueChanged(object sender, DependencyPropertyChangedEventArgs e) 
    { 

    } 
+0

C# 언어 사양은 우리에게 그 정적 a를 알려줍니다처럼 보인다 텍스트 순서로 초기화되므로 MinValProperty가 먼저 선언되도록 배열을 다시 시작하십시오. –

+0

그리고 내가해야 할 일은 무엇입니까? 코드를 보여주세요 .. Thanks – cheziHoyzer

+0

나는 아래의 강압 응답에 이미 투표했다. 그것은 완전히 강력한 전략입니다. –

답변

4

은 기본적으로 당신이 당신의 종속성 속성에 대한 방법을 강요 추가 : 여기

는 코드입니다. 기본 값은 메타 데이터에 있습니다. 그러나 XAML이로드 된 후에 값이 표시되기 전에 값이 서로 반응하도록하고 강요하면됩니다.

private static void OnMinValChanged(DependencyObject d, 
    DependencyPropertyChangedEventArgs e) 
{ 
    ((NumericHexUpDown)d).CoerceValue(ValueProperty); 
} 
private static void OnValueChanged(DependencyObject d, 
    DependencyPropertyChangedEventArgs e) 
{ 
    ((NumericHexUpDown)d).CoerceValue(MinValProperty); 
} 
private static object CoerceMinVal(DependencyObject d, 
    object value) 
{ 
    double min = ((NumericHexUpDown)d).MinVal; 
    return value; 
} 
private static object CoerceValue(DependencyObject d, 
    object value) 
{ 
    double min = ((NumericHexUpDown)d).MinVal; 
    double val = (double)value; 
    if (val < min) return min; 
    return value; 
} 

는 메타 데이터 생성자는이

public static readonly DependencyProperty MinValProperty = DependencyProperty.Register(
    "MinVal", typeof(int), typeof(NumericHexUpDown), 
    new FrameworkPropertyMetadata(
      0, 
      new PropertyChangedCallback(OnMinimumChanged), 
      new CoerceValueCallback(CoerceMinimum) 
     ), 
); 
public static readonly DependencyProperty ValueProperty = DependencyProperty.Register(
    "Value", typeof(int), typeof(NumericHexUpDown), 
    new FrameworkPropertyMetadata(
      0, 
      new PropertyChangedCallback(OnValueChanged), 
      new CoerceValueCallback(CoerceValue) 
     ), 
); 

참조

+0

좋은 !!! It 's work 고마워요 !!!! – cheziHoyzer