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();
}
}
도움을 주셔서 감사합니다.
나는 그 길을 피하려고했지만 결국 길을가는 것을 끝내었다. – eoldre
이것은 멀티 바인딩을 수행하는 방법을 알고있는 방법입니다 ... 나는 그것을 테스트하고 작동합니다. –