Using multibinding to set custom attached property in WPF

耗尽温柔 提交于 2019-12-11 08:14:51

问题


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.

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>

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!


回答1:


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>

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



来源:https://stackoverflow.com/questions/12957391/using-multibinding-to-set-custom-attached-property-in-wpf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!