1

사용자 지정 컨트롤에 사용하는 데이터 템플릿 집합이 있습니다. 그것은 잘 작동하지만 데이터에 바인드하고 값의 최소/최대를 기준으로 값을 가질 수 있기를 원합니다. 나는 다음과 같은 값 계산기 만들었습니다DataTemplate에서 값 변환기 조작

public class ScaleValueConverter : IValueConverter 
{ 
    /// <summary> 
    /// The property to use the value of 
    /// </summary> 
    public string ValueProperty { get; set; } 

    /// <summary> 
    /// The minimum value to be scaled against. Will become 0% 
    /// </summary> 
    public int MinValue { get; set; } 

    /// <summary> 
    /// The maximum value to be scaled against. Will become 100%. 
    /// </summary> 
    public int MaxValue { get; set; } 


    #region IValueConverter Members 

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     var type = value.GetType(); 
     var property = type.GetProperty(ValueProperty); 

     if (property == null) 
      return 0; 

     var result = System.Convert.ToDecimal(property.GetValue(value, null)); 

     //TODO: Scale stuff 

     return result + 100; 
    } 

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

    #endregion 
} 

목적은 일반적인 값 변환을하는 것입니다, 단순히 XAML에서 값 변환기, 바인딩 소스 객체를 제공하고, 물건을 분류합니다.

템플릿 컨트롤에서 만든 값 변환기에 액세스 할 수 없기 때문에이 방법을 잘 모르겠습니다.

나는 다음과 같이 대략 일하는 것이 뭔가를 찾고 있어요 :

 public override void OnApplyTemplate() 
    { 
     base.OnApplyTemplate(); 

     //Get Value Converters 
     var topScaleValueConverter = GetTemplateChild("TopScaleValueConverter"); 
     var bottomScaleValueConverter = GetTemplateChild("BottomScaleValueConverter"); 

     //Setup value converter Min/Max/ValueProperty here 
    } 

가 이상적으로 그들이 내 템플릿의 일부가 될 것입니다, 나는 파트로 추출 할 수 있지만 그 일을 나타나지 않습니다.

누구든지이 유형의 동작을 얻으려면 올바른 방향으로 나를 가리킬 수 있습니까?

감사

트리스탄

편집 : 내가 그들을 의존성 삽입 할 수있는 것이 좋은 것 같아요. 이것이 가능한지 아는 사람 있습니까?

답변

0

DependDencyObject에서 ScaleValueConverter를 파생시키고 속성을 종속성 속성으로 구현합니다.

public class ScaleValueConverter : DependencyObject, IValueConverter 
    { 

     public double MinValue 
     { 
      get { return (double)GetValue(MinValueProperty); } 
      set { SetValue(MinValueProperty, value); } 
     } 

     public static readonly DependencyProperty MinValueProperty = 
      DependencyProperty.Register("MinValue", typeof(double), typeof(ScaleValueConverter), new PropertyMetadata(0.0d)); 


     public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
     { 
      double result = double.Parse(value.ToString()) 

      if (result < MinValue) 
      { 
       result = MinValue; 
      } 

      return result; 
     } 
    } 

그러면 VM을 통해 속성에 데이터를 "주입"할 수 있습니다. 즉, 변환기를 다른 종속성 개체와 동일하게 취급하고 평소대로 바인딩하십시오.

+0

안녕하세요, 감사합니다. 이것은 업무를 수행해야하는 것처럼 보이지만, 특정 구현에 다소 매달 렸습니다. ScaleValueConverters는 ItemsControl의 Item 템플릿에 바인딩되지만 Min/Max를 계산하려면 부모 세트에 대한 액세스가 필요합니다. 어떻게해야합니까? – Tristan

+0

아마 나는 변환기로 이것을 처리하지 않을 것이고, 오히려 min과 max를 포함하도록 데이터 모델을 업데이트 할 것입니다. 복잡한 아이템을 가지고있을 때 작동하는 또 다른 접근법은 템플릿 컨트롤로 그것을 빌드하는 것입니다. 생각할 거리. – Brian