Set the GridViewColumnHeader style from the ListView style

In one of my projects, I inherited a ListView and redefined the style by installing a new control pattern. I also redefined the column heading style. So far I have found two ways to do this:

1) By setting the style key and referring to the style in the GridView:

<Style TargetType="{x:Type GridViewColumnHeader}" x:Key="MyHeaderStyle"> <Setter Property="Background" Value="Wheat" /> </Style> <GridView ColumnHeaderContainerStyle="{StaticResource MyHeaderStyle}"> 

2) Without setting the style key for the above style. Now I do not need to reference the style in the GridView, but it also overrides ALL list headers in my application, regardless of the type of list.

Since I use many lists in my application, I would like to do this in a third and more flexible way; setting the GridView.ColumnHeaderContainerStyle from the ListView style. This way, I will not need to refer to the header style in each GridView. Here is a simplified version of XAML:

 <Window.Resources> <Style TargetType="{x:Type GridViewColumnHeader}" x:Key="MyHeaderStyle"> <Setter Property="Background" Value="Wheat" /> </Style> <Style TargetType="{x:Type list:MyListView}"> <Setter Property="GridView.ColumnHeaderContainerStyle" Value="{StaticResource MyHeaderStyle}" /> <Setter Property="Background" Value="Linen" /> </Style> </Window.Resources> <list:MyListView> <list:MyListView.View> <GridView> <GridViewColumn Header="Column1" /> <GridViewColumn Header="Column2" /> </GridView> </list:MyListView.View> </list:MyListView> 

This, unfortunately, does not set the header style ... If I made this change in the XAML above, it works:

 <GridView ColumnHeaderContainerStyle="{StaticResource MyHeaderStyle}"> 

Any ideas?

+6
source share
2 answers

Thanks to snurre for pointing me in the right direction. I found a way to do what I wanted.

You do not need to place the "Resources" section in the ListView (this type of custom tag for each ListView is what I would like to get rid of first). Instead, resources can be moved to the ListView style.

Here is the updated XAML that works exactly the way I want:

 <Window.Resources> <Style TargetType="{x:Type GridViewColumnHeader}" x:Key="MyHeaderStyle"> <Setter Property="Background" Value="Wheat" /> </Style> <Style TargetType="{x:Type list:MyListView}"> <Style.Resources> <Style TargetType="{x:Type GridViewColumnHeader}" BasedOn="{StaticResource MyHeaderStyle}" /> </Style.Resources> <Setter Property="Background" Value="Linen" /> </Style> </Window.Resources> <list:MyListView> <list:MyListView.View> <GridView> <GridViewColumn Header="Column1" x:Name="col1" /> <GridViewColumn Header="Column2" x:Name="col2" /> </GridView> </list:MyListView.View> </list:MyListView> 
+6
source

If the key is missing, it applies to all elements of the specified TargetType . If your style has a key, you should use it explicitly:

 <GridViewColumn HeaderContainerStyle="{StaticResource MyHeaderStyle}" Header="Column1"/> 

Alternatively, you can set the style in your ListView , so it only applies to items inside it:

 <list:MyListView> <list:MyListView.Resources> <Style TargetType="{x:Type GridViewColumnHeader}"> <Setter Property="Background" Value="Wheat" /> </Style> </list:MyListView.Resources> </list:MyListView> 
+3
source

Source: https://habr.com/ru/post/905071/


All Articles