klashagelqvist
klashagelqvist

Reputation: 1261

WPF binding to parent content in datatrigger

I have a custom control with a style. The control is bound to a property in my viewmodel

<controls:PromoAlarmBox Content="{Binding Controller.IOGRP1W.Value}"/>

I want to create a datatrigger which changes the color of the control depending on the bound value and this works

<Style TargetType="{x:Type local:PromoAlarmBox}">          
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ContentControl">
                    <Rectangle x:Name="PART_rectangle" VerticalAlignment="Stretch" Fill="Yellow" Stroke="Black" Height="20" Width="20"/>

                    <ControlTemplate.Triggers>
                            <DataTrigger Binding="{Binding Controller.IOGRP1W.Value, UpdateSourceTrigger=PropertyChanged}" Value="1">

                            <Setter Property="Fill" TargetName="PART_rectangle" Value="Red" />

                        </DataTrigger>
                    </ControlTemplate.Triggers>    
                </ControlTemplate>
            </Setter.Value>
        </Setter>           
    </Style>

So far so good. I can make it work if I refer to the same property in the datatrigger as the control is bound to. My problem is that I have multiple instances of the same control bound to different values and I don't want to create a new style for each one of them so my question is how can I bind to the bound value of the control in the datatrigger.

Upvotes: 3

Views: 2260

Answers (3)

klashagelqvist
klashagelqvist

Reputation: 1261

Found It. Love WPF but it seems that you can spend weeks just studying different binding expressions

 <DataTrigger Binding="{Binding Path=Content, RelativeSource={RelativeSource Mode=Self}}" Value="1">

Upvotes: 3

mario
mario

Reputation: 1248

Maybe you could try this:

<ControlTemplate.Triggers>
    <Trigger Property="Content" Value="1">
        <Setter Property="Fill" TargetName="PART_rectangle" Value="Red" />
    </Trigger>
</ControlTemplate.Triggers> 

This way the Trigger should bind to the Content property of the PromoAlarmBox.

Upvotes: 0

Alexis
Alexis

Reputation: 825

You should use PriorityBinding to achieve the target behavior

Upvotes: -1

Related Questions