wpf – 从ListView样式中设置GridViewColumnHeader样式

在我的一个项目中,我继承了ListView并通过设置新的控件模板来覆盖样式.我还重写了列标题样式.到目前为止,我已经找到了两种方法:

1)通过设置样式键并引用GridView中的样式:

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

<GridView ColumnHeaderContainerStyle="{StaticResource MyHeaderStyle}">

2)不为上述样式设置样式键.现在我不需要在GridView中引用样式,但它也会覆盖我的应用程序中的所有listview头,而不依赖于listview类型.

由于我在我的应用程序中使用了很多列表视图,因此我希望以第三种更灵活的方式实现这一点;通过在ListView样式中设置GridView.ColumnHeaderContainerStyle.这样我就不需要在每个GridView中引用标题样式.这是目前为止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>

遗憾的是,这并未设置标题样式…如果我对上面的XAML进行了此更改,则可以:

<GridView ColumnHeaderContainerStyle="{StaticResource MyHeaderStyle}">

有任何想法吗?

解决方法

感谢snurre指出我正确的方向.我找到了一种方法来完成我想要的东西.

您不需要在ListView中放置Resources部分(每个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>

相关文章

Android 如何解决dialog弹出时无法捕捉Activity的back事件 在...
Android实现自定义带文字和图片的Button 在Android开发中经常...
Android 关于长按back键退出应用程序的实现最近在做一个Andr...
android自带的时间选择器只能精确到分,但是对于某些应用要求...