On my Window there are several GroupBox Controls, each containing a Grid Control. To those Grids I want to asign a Style. But only to those Grids that are directly in a GroupBox, all other Grids should not be affected.
I have tried the following, which does not work as GetType() is no property.
<Style TargetType="Grid">
<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=Self}, Path=Parent.GetType()}" Value="{x:Type GroupBox}">
<!-- <Setter Property="..." Value="..."/> -->
</DataTrigger>
</Style.Triggers>
</Style>
I have found a workaround, but it's not really a beautiful solution, as I have to modify the GroupBoxes:
<Style TargetType="GroupBox">
<Setter Property="Tag" Value="blub"/>
</Style>
<Style TargetType="Grid">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=Parent.Tag, RelativeSource={RelativeSource Mode=Self}}" Value="blub">
<!-- <Setter Property="..." Value="..."/> -->
</DataTrigger>
</Style.Triggers>
</Style>
Obviously I could set the style for each Grid manually, but I'm trying to avoid that, as there are quite a lot of them. I hope you can find a way to make the first example work.
<DataTrigger Binding="{Binding Path=Parent.Tag, RelativeSource={RelativeSource Mode=Self}}" Value="blub">
This code would not work, because type of Mode is actually BindingMode which is an Enumeration, and none of it's member is Self. So this assignment Mode=Self is wrong in your code. To know the possible values of Mode, click this.
The correct way to write this is,
<DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type GroupBox}}, Path=Tag}" Value="blub">
And of course, for this to work, you've to keep that Style for GroupBox which you've already written.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With