簡體   English   中英

使用multibinding在WPF中設置自定義附加屬性

[英]Using multibinding to set custom attached property in WPF

我有一個MVVM WPF項目,我試圖使用multiconverter設置自定義附加屬性的值,應該傳遞DataContext(ViewModel)的屬性和元素上的另一個自定義附加屬性。

我可以使用xaml屬性語法直接將屬性綁定到viewmodel,但是我無法理解如何使用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的代碼。

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

謝謝你的盡心幫助!

你可以嘗試這個並適應你的代碼:

     <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”或主窗口的視圖模型管理此行為。 也許可能更簡單,或觸發。 希望這會對你有所幫助......

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM