I have a Style in my Window.Resources:
<Style x:Key="Header" TargetType="GridViewColumnHeader">
<Setter Property="Content">
<Setter.Value>
<StackPanel Orientation="Horizontal">
<Label Width="80" HorizontalContentAlignment="Center" Content="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=GridViewColumnHeader, AncestorLevel=1}, Path=Tag}" />
<StackPanel Orientation="Vertical">
<Polygon Name="P_up" Points="0,5 10,5, 5,0" Stroke="Black" Fill="Black" Margin="3" Visibility="Visible"/>
<Polygon Name="P_down" Points="0,0 10,0, 5,5" Stroke="Black" Fill="Black" Margin="3" Visibility="Hidden"/>
</StackPanel>
</StackPanel>
</Setter.Value>
</Setter>
<EventSetter Event="Click" Handler="Header_Click"/>
</Style>
When I set this style to my ListView (GridViewColumnHeader) like this:
<ListView Height="300" x:Name="lv" ItemsSource="{Binding PLCs}">
<ListView.View>
<GridView>
<GridViewColumn Width="120">
<GridViewColumnHeader Name="myNewText" Tag="test" Style="{StaticResource Header}"/>
</GridViewColumn>
<GridViewColumn Width="120">
<GridViewColumnHeader Name="myNewText2" Tag="test2" Style="{StaticResource Header}"/>
</GridViewColumn>
<GridViewColumn Width="120">
<GridViewColumnHeader Tag="test32" Style="{StaticResource Header}"/>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
The binding to the ancestors Tag works and the Polygons are shown, but only for the last one of the GridViewColumns, the first two are staying blank. Can anyone tell me what I am doing wrong? Since the code is the same for all three columns I assumed it would provide me withe the same results for all three. I guess either the Content binding of the Label or the TargetType of the Style is not correct.
Thanks for your help.
You should use the ContentTemplate
property:
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<! -- ...place your stack panel here -->
</DataTemplate>
</Setter.Value>
</Setter>
Doing so, you define a template for each column header. The template will instantiate the content for each column header independently.
If you use the Content
property, you set the same stack panel as the content to all the column headers. This won't work - only the last one will win. The stack panel's parent will be automatically set to the last header. It's like:
header1.Content = your_stack_panel;
your_stack_panel.Parent = header1;
header2.Content = your_stack_panel;
your_stack_panel.Parent = header2;
header3.Content = your_stack_panel;
your_stack_panel.Parent = header3;