Bind trigger value to property

I am trying to set up a trigger, so if two values โ€‹โ€‹match a color change, it is easy when the item to be matched is static and can be placed directly in the xaml, but not when the item to be compared is dynamic, such as own. Basically, somehow, to bind the trigger value to the property?

Example. The error says the value cannot use binding. This makes me think the value should be static.

<TextBlock Name="MyTextBlock" Text="{Binding someProp}">
    <TextBlock.Resources>
        <Style TargetType="TextBlock">
            <Style.Triggers>
                <DataTrigger Binding="{Binding ElementName=MyTextBlock, Path=Text}" Value="{Binding someOtherProperty}">
                    Do some stuff here
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </TextBlock.Resources>
</Textblock>

      

EDIT: Updated to a data trigger, but the problem remains.

+3


source to share


1 answer


To do this, you can use DataTriggers

as shown in the example below ( TextBlock

named txtBlock

color changes depend on the value "R" or "N"):

<Style.Triggers>
    <DataTrigger Binding="{Binding ElementName=txtBlock,Path=Text}" Value="R">
            <Setter Property="Background" Value="#f9f9f9" />
            <Setter Property="Foreground" Value="Red" />
    </DataTrigger>
    <DataTrigger Binding="{Binding ElementName=txtBlock,Path=Text}" Value="N">
        <Setter Property="Background" Value="Yellow" />
        <Setter Property="Foreground" Value="Black" />
    </DataTrigger>
</Style.Triggers>

      



The solution works for any target dataset used in the state. For a more complex condition (like a variable used in a condition block, etc.), you can implement a value converter and binding in code as shown in the example: Binding in WPF DataTrigger Value . Alternatively, you can consider MultiDataTrigger

either DataTrigger

using MultiBinding

(re: MultiDataTrigger vs DataTrigger with multi-linked ).

Hope this helps.

+3


source







All Articles