noontz
noontz

Reputation: 2017

How to access a MultiBinding defined in Application Resources

I´d like to reuse a MultiBinding and tried to use this solution, but I can´t seem to reach IsEnabled from a Setter Property.

So I tried this approach, but no cigar :

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 > These two attempts will not compile :

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

Any ideas?

EDIT: After accepting Funcs answer I guess this has allready been answered in the linked thread. Sorry..

Upvotes: 1

Views: 553

Answers (1)

Funk
Funk

Reputation: 11221

Try wrapping it in 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}"/>

Note TargetType="Control" making it generic.

Upvotes: 2

Related Questions