【问题标题】:How to access a MultiBinding defined in Application Resources如何访问应用程序资源中定义的 MultiBinding
【发布时间】:2016-07-20 12:01:08
【问题描述】:

我想重用 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 > 这两次尝试都不会编译:

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

有什么想法吗?

编辑:在接受 Funcs 回答后,我想这已经在链接的线程中得到了回答。对不起..

【问题讨论】:

    标签: c# wpf xaml


    【解决方案1】:

    尝试用 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" 使其通用。

    【讨论】:

    • Style="{StaticResource ControlEnabler}" 会给你一个无效的演员表。正如我所写,我已经尝试过这种方法,如果您尝试在本地级别执行此操作,您会发现您无法从 Setters 属性访问 IsEnabled
    • @noontz 有效,试过了。您的 MultiBinding 需要返回一个布尔值。
    • 感谢您的努力。你能告诉我你的 BooleanMultiConverter 的代码吗?我返回一个布尔值,但我什至无法编译您的 app.xaml sn-p:x:Key="OnOffBinding" 部分上的错误 MC3032
    • 它也可以在这里工作。不知道以前出了什么问题,但现在我有了我想要的东西,而且似乎我的问题是多余的。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-17
    • 1970-01-01
    • 1970-01-01
    • 2011-07-25
    • 1970-01-01
    • 2018-12-14
    • 1970-01-01
    相关资源
    最近更新 更多