在 MultiDataTrigger 中本地使用 IValueConverter

Use IValueConverter locally inside MultiDataTrigger

我想创建一个 MultiDataTrigger。在其中一种情况下,我需要使用转换器。由于它只在那里使用,我不想将转换器定义为资源而只在本地使用它。

所以我制作了这个 MultiDataTrigger:

<MultiDataTrigger>
    <MultiDataTrigger.Conditions>
        <Condition Binding="Property1" Value="Value1" />
        <Condition ... />
    </MultiDataTrigger.Conditions>
    <Setter Property="SomeProp" Value="SomeVal"/>
</MultiDataTrigger>

和此绑定以避免将转换器定义为资源(​​基于 ):

<Binding Path="Property2">
    <Binding.Converter>
        <converterNamespace:MyConverter/>
    </Binding.Converter>
</Binding>

并将两部分放在一起:

<MultiDataTrigger>
    <MultiDataTrigger.Conditions>
        <Condition Binding="Property1" Value="Value1" />
        <Condition Value="Value2">
            <Binding Path="Property2">                 <!-- Compiler -->
                <Binding.Converter>                    <!-- does     -->
                    <converterNamespace:MyConverter/>  <!-- not      -->
                </Binding.Converter>                   <!-- like     -->
            </Binding>                                 <!-- this!    -->
        </Condition>
    </MultiDataTrigger.Conditions>
    <Setter Property="SomeProp" Value="SomeVal"/>
</MultiDataTrigger>

但是编译器报错:"Type 'Condition' does not support direct content."(原文:"Der Typ 'Condition' unterstützt keine direkten Inhalte.")

有没有办法让 MultiDataTrigger 工作而无需将转换器定义为资源?

您的 XAML 缺少 <Condition.Binding> 标签。除此之外,Binding="{Property1}"也是无效的。

<MultiDataTrigger>
    <MultiDataTrigger.Conditions>
        <Condition Binding="{Binding Property1}" Value="Value1" />
        <Condition Value="Value2">
            <Condition.Binding>                   <!-- here -->
                <Binding Path="Property2">
                    <Binding.Converter>
                        <converterNamespace:MyConverter/>
                    </Binding.Converter>
                </Binding>
            </Condition.Binding>
        </Condition>
    </MultiDataTrigger.Conditions>
</MultiDataTrigger>