DerApe
DerApe

Reputation: 3175

Style trigger from parent control

Is it possible to use a style trigger from another control?

I have Border control which lives in the indicator part of each row of a grid (the indicator is the part on the very left with the little arrow). I want to set the background depending if the row is selected. So I created a style:

<controls:SelectionConverter x:Key="SelectionConverter" />
<Style x:Key="SelectionStyle" TargetType="Border">
  <Setter Property="Background" Value="{Binding Converter={StaticResource SelectionConverter}}"/>
  <Style.Triggers>
    <!-- here I want to have a trigger which reacts on a property of the grid control -->
  </Style.Triggers>
</Style>

The border control then will use the style (in fact there are 3 border controls).

The SelectionConverter will return the correct color depending on the row (that works fine).

The problem is that the background is not updated when I select a different cell (which does make sense, because there is no trigger when to update it).

Is it possible to setup a trigger of the parent control?

Something alone the line

<Trigger Property="ParentControl.SelectionHasChanged" Value="True"></Trigger>

Upvotes: 0

Views: 1127

Answers (1)

Chris Mack
Chris Mack

Reputation: 5208

You should be able to use ElementName in your Binding to achieve this. For example, the following binds to the IsEnabled property of a Grid and sets the Background property of the Border to red when this is true:

<Grid x:Name"main_grid">
    ...
    <controls:SelectionConverter x:Key="SelectionConverter" />
    <Style x:Key="SelectionStyle" TargetType="Border">
        <Setter Property="Background" Value="{Binding Converter={StaticResource SelectionConverter}}"/>
        <Style.Triggers>
            <DataTrigger Binding="{Binding IsEnabled, ElementName=main_grid}" Value="True">
                <Setter Property="Background" Value="Red" />
            </DataTrigger>
        </Style.Triggers>
    </Style>
    ...
</Grid>

Upvotes: 1

Related Questions