2012-10-18 2 views
1

MVVM WPF 프로젝트에서 DataContext (ViewModel) 및 다른 사용자 지정 속성을 전달해야하는 다중 변환기를 사용하여 사용자 지정 연결된 속성 값을 설정하려고합니다. 요소에 연결된 속성.멀티 바인딩을 사용하여 WPF에서 사용자 지정 연결된 속성 설정

xaml 특성 구문을 사용하여 직접 속성을 viewmodel에 바인딩 할 수 있지만 다중 변환기를 사용하여 연결된 속성의 값을 설정하는 방법을 이해하는 데 문제가 있습니다.

<StackPanel> 
    <StackPanel.Resources> 
     <example:HelpTextConverter x:Key="ConvertHelpText"></example:HelpTextConverter> 
    </StackPanel.Resources> 

    <!-- shows binding the help text properties directly to the ViewModel's property --> 
    <Border example:HelpTextProperties.HelpText="{Binding HelpText}"></Border> 

    <!--how to set the HelpText attached property as the result of passing the DataContext.HelpText and the HelpTextProperties.ShowHelpText property to the HelpTextConverter?--> 
    <Border> 
     <example:HelpTextProperties.HelpText> 
      <!--does not compile--> 
     </example:HelpTextProperties.HelpText> 
    </Border> 

</StackPanel> 

코드 예를 들어 첨부 된 속성 및 아래의 IMultiValueConverter.

class HelpTextProperties 
{ 
    public static readonly DependencyProperty ShowHelpTextProperty = 
     DependencyProperty.RegisterAttached("ShowHelpText", typeof(bool), typeof(HelpTextProperties), 
      new UIPropertyMetadata(false)); 

    public static bool GetShowHelpText(UIElement target) 
    { 
     return (bool)target.GetValue(ShowHelpTextProperty); 
    } 

    public static void SetShowHelpText(UIElement target, bool value) 
    { 
     target.SetValue(ShowHelpTextProperty, value); 
    } 

    public static readonly DependencyProperty HelpTextProperty = 
     DependencyProperty.RegisterAttached("HelpText", typeof(LabelVM), typeof(HelpTextProperties), 
     new UIPropertyMetadata(null)); 

    public static LabelVM GetHelpText(UIElement target) 
    { 
     return (LabelVM)target.GetValue(ShowHelpTextProperty); 
    } 

    public static void SetHelpText(UIElement target, LabelVM value) 
    { 
     target.SetValue(ShowHelpTextProperty, value); 
    } 
} 

class HelpTextConverter : IMultiValueConverter 
{ 
    /// <summary> 
    /// returns the label in values[0] if values[1] is true 
    /// </summary> 
    public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     LabelVM labelVM = (LabelVM)values[0]; 
     if (values[0] == DependencyProperty.UnsetValue) { return null; } 
     if (values[1] is bool) 
     { 
      if ((bool)values[1]) { return labelVM; } 
     } 
     return null; 
    } 

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

도움을 주셔서 감사합니다.

답변

3

이 작업을 시도하고 코드에 적응할 수 :

 <Border> 
     <example:HelpTextProperties.HelpText> 
      <MultiBinding Converter="{StaticResource ResourceKey=ConvertHelpText}"> 
       <Binding Path="HelpText"/> <!--The property that you wants, from DataContext or Dependency Property--> 
       <Binding Path="ShowLabel"/> <!--Same thing, the property that you wants--> 
      </MultiBinding> 
     </example:HelpTextProperties.HelpText> 
    </Border> 

이 설정 multibinding을 변환이지만, 또한 난 당신이 당신의 "MainViewModel"이 문제가 나에 대한 뷰 모델을 관리 할 수 ​​있다고 생각 메인 윈도우. 어쩌면 더 간단 할 수도 있고 어쩌면 방아쇠를 당길 수도 있습니다. 희망이 당신에게 도움이 될 것입니다 ...

+0

나는 그 길을 피하려고했지만 결국 길을가는 것을 끝내었다. – eoldre

+0

이것은 멀티 바인딩을 수행하는 방법을 알고있는 방법입니다 ... 나는 그것을 테스트하고 작동합니다. –

관련 문제