1

マルチコンバーターを使用してカスタム添付プロパティの値を設定しようとしているMVVMWPFプロジェクトがあります。これには、DataContext(ViewModel)のプロパティと要素の別のカスタム添付プロパティを渡す必要があります。

xaml属性構文を使用してプロパティをビューモデルに直接バインドできますが、マルチコンバーターを使用してアタッチされたプロパティの値を設定する方法を理解するのに問題があります。

<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();
    }
}

あなたが提供できるどんな助けにも感謝します!

4

1 に答える 1

4

これを試して、コードに適応させることができます。

     <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>

これはマルチバインディングコンバーターを設定するためのものですが、この動作は「MainViewModel」またはメインウィンドウのビューモデルから管理できると思います。多分もっと簡単かもしれないし、多分トリガーするかもしれない。これがあなたのお役に立てば幸いです...

于 2012-10-18T17:12:38.663 に答える