wpfdata-bindingivalueconvertermultidatatrigger

Use IValueConverter locally inside MultiDataTrigger


I want to create a MultiDataTrigger. Inside one of the conditions I need to use a converter. Since it is only used there, I do not want to define the converter as a resource but only use it locally.

So I made this MultiDataTrigger:

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

and this binding to avoid defining the converter as resource (based on https://stackoverflow.com/a/2304330/5333340):

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

and put the two pieces together:

<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>

But the compiler complains: "Type 'Condition' does not support direct content." (original: "Der Typ 'Condition' unterstützt keine direkten Inhalte.")

Is there a way to get the MultiDataTrigger working without needing to define the converter as resource?


Solution

  • Your XAML is missing the <Condition.Binding> tag. Besides that, Binding="{Property1}" is also invalid.

    <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>