【问题标题】:Using multibinding to set custom attached property in WPF使用多重绑定在 WPF 中设置自定义附加属性
【发布时间】:2012-10-18 14:46:48
【问题描述】:

我有一个 MVVM WPF 项目,我正在尝试使用多转换器设置自定义附加属性的值,它应该传递 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();
    }
}

感谢您提供的任何帮助!

【问题讨论】:

    标签: c# wpf xaml dependency-properties


    【解决方案1】:

    你可以试试这个并适应你的代码:

         <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”或主窗口的视图模型中管理此行为。也许可以更简单,或者触发。希望这对您有所帮助...

    【讨论】:

    • 我本想避开那条路,但最终还是走上了这条路。
    • 这是我所知道的进行多重绑定的方式...我对其进行了测试并且可以正常工作
    猜你喜欢
    • 1970-01-01
    • 2011-11-23
    • 1970-01-01
    • 2011-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-14
    相关资源
    最近更新 更多