6

In one of my projects I have inherited a ListView and overridden the style by setting a new control template. I have also overridden the column header style. So far I have found two ways to do this:

1) By setting a style key and referencing the style in the GridView:

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

<GridView ColumnHeaderContainerStyle="{StaticResource MyHeaderStyle}">

2) By not setting a style key for the above style. Now I don't need to reference the style in the GridView, BUT it also overrides ALL listview headers in my application independent of listview type.

Since I use many listviews in my application, I would like to accomplish this in a third and more flexible way; by setting the GridView.ColumnHeaderContainerStyle from inside a ListView style. That way I would not need to reference the header style in each GridView. Here is a simplified version of the XAML so far:

<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 make this change to the XAML above, it works:

<GridView ColumnHeaderContainerStyle="{StaticResource MyHeaderStyle}">

Any ideas?

4

2 回答 2

6

感谢snurre为我指明了正确的方向。我找到了一种方法来完成我想要的。

您不需要将 Resources 部分放在 ListView 中(每个 ListView 的那种自定义标签是我首先想要摆脱的)。可以将资源移至 ListView 样式。

这是完全按照我想要的方式工作的更新后的 XAML:

<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>
于 2012-01-05T07:05:43.887 回答
3

如果没有键,它适用于指定的所有元素TargetType。如果您的样式有键,则必须明确使用它:

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

或者,您可以在 中设置样式ListView,因此它仅适用于其中的元素:

<list:MyListView>
    <list:MyListView.Resources>
        <Style TargetType="{x:Type GridViewColumnHeader}">
            <Setter Property="Background" Value="Wheat" />
        </Style>
    </list:MyListView.Resources>
</list:MyListView>
于 2012-01-04T10:08:33.737 回答