如何自定义和重用DataGridColumnHeader样式?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何自定义和重用DataGridColumnHeader样式?相关的知识,希望对你有一定的参考价值。
我正在尝试自定义DataGrid的列标题以显示子列标题,如以下屏幕截图所示:
alt text http://img297.imageshack.us/img297/464/columnheaders.gif
我为2个子列创建了一个样式,如下面的XAML所示:
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"
xmlns:primitives="clr-namespace:System.Windows.Controls.Primitives;assembly=System.Windows.Controls.Data"
xmlns:sl="clr-namespace:UI" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" x:Class="UI.ColumnHeaderGrid"
mc:Ignorable="d">
<UserControl.Resources>
<Style x:Key="SplitColumnHeaderStyle" TargetType="primitives:DataGridColumnHeader">
<Setter Property="Foreground" Value="#FF000000"/>
<Setter Property="HorizontalContentAlignment" Value="Center"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="IsTabStop" Value="False"/>
<Setter Property="SeparatorBrush" Value="#FFC9CACA"/>
<Setter Property="Padding" Value="4"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="primitives:DataGridColumnHeader">
<Grid x:Name="Root">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Rectangle x:Name="BackgroundRectangle" Fill="#FF1F3B53" Stretch="Fill" Grid.ColumnSpan="2"/>
<Rectangle x:Name="BackgroundGradient" Stretch="Fill" Grid.ColumnSpan="2">
<Rectangle.Fill>
<LinearGradientBrush EndPoint=".7,1" StartPoint=".7,0">
<GradientStop Color="#FCFFFFFF" Offset="0.015"/>
<GradientStop Color="#F7FFFFFF" Offset="0.375"/>
<GradientStop Color="#E5FFFFFF" Offset="0.6"/>
<GradientStop Color="#D1FFFFFF" Offset="1"/>
</LinearGradientBrush>
</Rectangle.Fill>
</Rectangle>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition Width="1"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Grid.ColumnSpan="3" Text="Headers" TextAlignment="Center"/>
<Rectangle Grid.Row="1" Grid.ColumnSpan="3" Fill="{TemplateBinding SeparatorBrush}" Height="1"/>
<TextBlock Grid.Row="2" Grid.Column="0" Text="Header 1" TextAlignment="Center"/>
<Rectangle Grid.Row="2" Grid.Column="1" Fill="{TemplateBinding SeparatorBrush}" Width="1"/>
<TextBlock Grid.Row="2" Grid.Column="2" Text="Header 2" TextAlignment="Center"/>
<Path x:Name="SortIcon" Grid.Column="2" Fill="#FF444444" Stretch="Uniform" HorizontalAlignment="Left" Margin="4,0,0,0" VerticalAlignment="Center" Width="8" Opacity="0" RenderTransformOrigin=".5,.5" Data="F1 M -5.215,6.099L 5.215,6.099L 0,0L -5.215,6.099 Z "/>
</Grid>
<Rectangle x:Name="VerticalSeparator" Fill="{TemplateBinding SeparatorBrush}" VerticalAlignment="Stretch" Width="1" Visibility="{TemplateBinding SeparatorVisibility}" Grid.Column="1"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</UserControl.Resources>
<data:DataGrid x:Name="LayoutRoot">
<data:DataGrid.Columns>
<data:DataGridTemplateColumn HeaderStyle="{StaticResource SplitColumnHeaderStyle}">
<data:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Border Grid.Column="0" BorderBrush="#FFC9CACA" BorderThickness="0,0,0,0">
<TextBlock Grid.Column="0" Text="{Binding GridData.Column1}"/>
</Border>
<Border Grid.Column="1" BorderBrush="#FFC9CACA" BorderThickness="1,0,0,0">
<TextBlock Grid.Column="0" Text="{Binding GridData.Column2}"/>
</Border>
</Grid>
</DataTemplate>
</data:DataGridTemplateColumn.CellTemplate>
</data:DataGridTemplateColumn>
</data:DataGrid.Columns>
</data:DataGrid>
现在我想重用和扩展这个样式来支持2-> 6个子列标题,但我不知道是否有办法做到这一点,比如ContentPresenter“覆盖”:
<Style x:Key="SplitColumnHeaderStyle" TargetType="primitives:DataGridColumnHeader">
<Setter property="Template">
<Setter.Value>
...
<ContentPresenter Content="{TemplateBinding Content}".../>
...
</Setter.Value>
</Setter>
</Style>
<Style x:Key="TwoSubColumnHeaderStyle" BasedOn="SplitColumnHeaderStyle">
<Setter property="Content">
<Setter.Value>
<Grid 2x2.../>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="ThreeSubColumnHeaderStyle" BasedOn="SplitColumnHeaderStyle">
<Setter property="Content">
<Setter.Value>
<Grid 2x3.../>
</Setter.Value>
</Setter>
</Style>
无论如何,请帮助我解决这些问题:
- 鉴于上面的模板,如何支持更多的子列标题而不必为每个创建新的模板?
- 假设上面的问题已经解决了。我怎么能在样式之外附加列名?
- 我看到XAML中的一些部件,属性和可视化规则只是原始Silverlight组件样式的副本,即BackgroundGradient,BackgroundRectangle,VisualStateManager ......它们必须在那里才能支持默认行为或效果但是...有没有人知道如何删除它们,但保留所有默认行为/效果?
请具体说明,因为我刚刚开始使用C#和Silverlight。
答案
这是一个很好的例子:http://weblogs.asp.net/dwahlin/archive/2009/06/11/customizing-silverlight-3-datagrid-headers.aspx
以上是关于如何自定义和重用DataGridColumnHeader样式?的主要内容,如果未能解决你的问题,请参考以下文章
打字稿混淆:如何使用自定义和绝对类型库扩展 Express `Request`?
如何使用 maven 部署 JavaFX 项目,包括自定义和非模块化依赖项?