嵌套的结合和转换管道 [英] Nested binding and piped conversion

查看:139
本文介绍了嵌套的结合和转换管道的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要少冗余XAML标记我试图让一个单选按钮类型选择控制被统称人口,即我使用的ItemsControl 与枚举为的ItemsSource 并创建一个DataTemplate表示哪个项目是通过检查项目的枚举值是否相同的当前设置所选

To have less redundant XAML markup i try to get a radiobutton-type selection control to be populated generically, i.e. i use an ItemsControl with an enum as ItemsSource and create a DataTemplate which shows which item is selected by checking whether the enum value of the item is the same as the current setting.

这不能单独使用一个简单的转换器或DataTrigger因为需要两个绑定,所以我创建做一个普通的 MutliValueConverter 来检查是否相等:

This alone cannot be done using a simple converter or DataTrigger because two bindings are needed, so i created a generic MutliValueConverter to check for equality:

<CheckBox.Visibility>
    <MultiBinding Converter="{StaticResource EqualityComparisonConv}">
        <Binding Path="Key"/>
        <Binding Path="DisplayMode_Current" Source="{x:Static local:App.Settings}"/>
    </MultiBinding>
</CheckBox.Visibility>

public class EqualityComparisonConverter : IMultiValueConverter
{
    #region IMultiValueConverter Members

    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        if (values.Length < 2) throw new Exception("At least two inputs are needed for comparison");
        bool output = (bool)values.Skip(1).Aggregate(values[0], (x1, x2) => { return x1.Equals(x2); });
        return output;
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        throw new NotSupportedException();
    }

    #endregion
}

这里的明显的问题是转换器返回boolean这也是我需要转换为能见度第一。

在另一只是一个转换器不工作,因为性能不依赖属性绑定中包装MultiBinding(因此不能有一个绑定分配给他们)。我能想到的几个解决方法像存储在某些标签属性布尔,这样我就可以使用,作为一个新的绑定源,但我会更感兴趣的是这样的

Wrapping the MultiBinding in another binding with just a converter does not work because the properties are not dependency properties (hence cannot have a binding assigned to them). I could think of a few workarounds like storing the bool in some Tag property, so i can use that as a new binding source, but i would be more interested in something like this:

<CheckBox.Visibility>
    <local:PipeConverter Converter="{StaticResource BooleanToVisibilityConv}">
        <MultiBinding Converter="{StaticResource EqualityComparisonConv}">
            <Binding Path="Key"/>
            <Binding Path="DisplayMode_Current" Source="{x:Static local:App.Settings}"/>
        </MultiBinding>
    </local:PipeConverter>
</CheckBox.Visibility>

本类需要时,在原来的绑定发生更改时更新它的输出,这将需要能够将其产值暴露在能见度属性,但我做的不知道如何实现两种。持斧到的一个问题是,有需要依赖属性从的DependencyObject 所以继承将是很好的,但是从绑定类继承也有道理,因为PipeConverter应该绑定并需要设置作为另一个依赖属性的值。

This class would need to update its output when changes in the original binding occur and it would need to be able to expose its output value to the Visibility property but i do not know how to achieve either. One problem one runs into is that there is a need for dependency properties so inheriting from DependencyObject would be nice, but inheriting from a Binding class would also make sense because the PipeConverter should bind and needs to be set as the value of another dependency property.

推荐答案

三个选项:



选项A :将您的布尔能见度在multivalueconverter(它只是一条线)

Three options:

Option A: Convert your bool to visibility in your multivalueconverter (its just one line)

public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
    if (values.Length < 2)
        throw new Exception("At least two inputs are needed for comparison");
    bool output = (bool)values.Skip(1).Aggregate(values[0], (x1, x2) =>
         { return x1.Equals(x2); });
    return output ? Visibility.Visible : Visibility.Collapsed;
}



选项B :编程使用现有booltovisibilityconverter


Option B: Use the existing booltovisibilityconverter programatically

public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
    if (values.Length < 2)
        throw new Exception("At least two inputs are needed for comparison");
    bool output = (bool)values.Skip(1).Aggregate(values[0], (x1, x2) =>
         { return x1.Equals(x2); });
    System.Windows.Controls.BooleanToVisibilityConverter booltovisibilityconverter = new System.Windows.Controls.BooleanToVisibilityConverter();
    return booltovisibilityconverter.Convert(output, System.Type.GetType("System.Boolean"), parameter, culture);
}

PS:你可能想缓存booltovisibilityconverter,而不是每次创建它。



选项C :你管器


管道值转换器在WPF

PS:Beaware这篇文章是很老

PS: You may want to cache the booltovisibilityconverter instead of creating it everytime.

Option C: Pipe your converters
Piping Value Converters in WPF
PS: Beaware that this article is quite old.

这篇关于嵌套的结合和转换管道的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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