如何访问应用程序资源中定义的MultiBinding

时间:2016-07-20 12:01:09

标签: c# wpf xaml

我想重复使用MultiBinding并尝试使用this solution,但我似乎无法从Setter属性中获取IsEnabled。

所以我尝试了这种方法,但没有雪茄:

的App.xaml

<Application x:Class="Test.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:model="clr-namespace:Test.MODEL"
         StartupUri="MainWindow.xaml">
<Application.Resources>     
    <view:BooleanConverter x:Key="BooleanConverter" />
    <view:BooleanMultiConverter x:Key="BooleanMultiConverter" />
    <MultiBinding x:Key="OnOffBinding" Converter="{StaticResource BooleanMultiConverter}" ConverterParameter="OR">
            <Binding Path="model:CustomerIsDefined" Converter="{StaticResource BooleanConverter}" />
            <Binding Path="model:CustomerIsConfirmed" Converter="{StaticResource BooleanConverter}" />
    </MultiBinding>
</Application.Resources>

MainWindow.xaml&gt;这两次尝试都不会编译:

<TextBox IsEnabled="{StaticResource OnOffBinding}"/>
<TextBox IsEnabled="{MultiBinding {StaticResource OnOffBinding}}" />

有什么想法吗?

编辑:接受Funcs回答后我猜这已经在链接线程中得到了解答。抱歉..

1 个答案:

答案 0 :(得分:2)

尝试将其包装在Style

<Application.Resources>
    <view:BooleanConverter x:Key="BooleanConverter" />
    <view:BooleanMultiConverter x:Key="BooleanMultiConverter" />
    <Style x:Key="ControlEnabler" TargetType="Control">
        <Setter Property="IsEnabled">
            <Setter.Value>
                <MultiBinding x:Key="OnOffBinding" Converter="{StaticResource BooleanMultiConverter}" ConverterParameter="OR">
                    <Binding Path="model:CustomerIsDefined" Converter="{StaticResource BooleanConverter}" />
                    <Binding Path="model:CustomerIsConfirmed" Converter="{StaticResource BooleanConverter}" />
                </MultiBinding>
            </Setter.Value>
        </Setter>
    </Style>
</Application.Resources>

MainWindow.xaml

<TextBox Style="{StaticResource ControlEnabler}"/>

注意TargetType="Control"使其成为通用。