0

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>
DerApe
  • 3,097
  • 2
  • 35
  • 55

1 Answers1

1

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>
Chris Mack
  • 5,148
  • 2
  • 12
  • 29
  • Thank you that is what I was looking for! – DerApe Dec 13 '17 at 07:52
  • In addition, can I also put the data trigger to an event of the grid somehow? As far as I get I can only assign animations to event trigger!? – DerApe Dec 13 '17 at 07:52
  • There is an example of including an animation within a `DataTrigger` here: https://stackoverflow.com/questions/80388/wpf-data-triggers-and-story-boards. Does that help? – Chris Mack Dec 13 '17 at 16:52
  • Thanks for the link. That's not just what I need. I want to have a trigger which hooks on an event, but where I'm able to set for example a property on the grid – DerApe Dec 14 '17 at 11:13
  • Could you give more specifics on the structure and what you need to happen (maybe add it into your question if it's a lot of info)? Also, are you using an MVVM/data binding approach? – Chris Mack Dec 14 '17 at 12:15