使用多重绑定在WPF中设置自定义附加属性 [英] Using multibinding to set custom attached property in WPF

查看:154
本文介绍了使用多重绑定在WPF中设置自定义附加属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个MVVM WPF项目,我试图在其中使用多转换器设置自定义附加属性的值,该属性应传递给DataContext(ViewModel)的属性以及元素上的另一个自定义附加属性。

I have a MVVM WPF project where I am attempting to set the value of a custom attached property using a multiconverter, which should be passed a property of the DataContext (ViewModel) and another custom attached property on the element.

我可以使用xaml属性语法将属性直接绑定到视图模型,但是我在理解如何使用多转换器设置附加属性的值时遇到了麻烦。

I can bind the property to the viewmodel directly using the xaml attribute syntax, but I'm having trouble understanding how to set the value of the attached property using a multiconverter.

<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的代码。

code for example attached properties and IMultiValueConverter below.

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

感谢您提供的任何帮助!

Thanks for any help you can offer!

推荐答案

您可以尝试此方法并适应您的代码:

You could try this and adapt to your code:

     <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或主窗口的视图模型。也许更简单,或者触发。希望这对您有帮助...

This is for set the multibinding converter, but also i think that you could manage this behavior from your "MainViewModel" or the view model for the main window. maybe could be simpler, or trigger maybe. Hope this will helpful to you...

这篇关于使用多重绑定在WPF中设置自定义附加属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆