猿问

WPF的DataGrid怎么实现多行表头

所谓的中国式的表头

             2011年

1月 2月 3月 。。。。。

这样的


ITMISS
浏览 957回答 2
2回答

UYOU

<Window.Resources><Style x:Key="CityNumStyle" TargetType="DataGridColumnHeader"><Setter Property="Template"><Setter.Value><ControlTemplate><Grid x:Name="Root"><!--<Rectangle x:Name="BackgroundGradient" Fill="#eee" Stretch="Fill" Grid.ColumnSpan="2" />--><ContentPresenter Content="区号" Grid.Row="2" VerticalAlignment="Center" HorizontalAlignment="Center" /></Grid></ControlTemplate></Setter.Value></Setter></Style><Style x:Key="CityNameStyle" TargetType="DataGridColumnHeader"><Setter Property="Foreground" Value="#222" /><Setter Property="HorizontalContentAlignment" Value="Left" /><Setter Property="VerticalContentAlignment" Value="Center" /><Setter Property="IsTabStop" Value="False" /><Setter Property="SeparatorBrush" Value="#CCC" /><Setter Property="Padding" Value="8" /><Setter Property="Template"><Setter.Value><ControlTemplate><Grid x:Name="Root"><Grid.ColumnDefinitions><ColumnDefinition /><ColumnDefinition Width="auto" /></Grid.ColumnDefinitions><!--<Rectangle x:Name="BackgroundRectangle" Fill="#eee" Stretch="Fill" Grid.ColumnSpan="2" />--><Grid HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"><Grid.RowDefinitions><RowDefinition Height="20" /><RowDefinition Height="1" /><RowDefinition Height="20" /></Grid.RowDefinitions><Grid.ColumnDefinitions><ColumnDefinition Width="100" /><ColumnDefinition Width="1" /><ColumnDefinition Width="100" /></Grid.ColumnDefinitions><!--设置标题名称--><ContentPresenter Content="省份城市" VerticalAlignment="Center" HorizontalAlignment="Center" Grid.ColumnSpan="3" /><!--设置间隔线--><Rectangle Fill="#ccc" VerticalAlignment="Stretch" Height="1" Visibility="Visible" Grid.Row="1" Grid.ColumnSpan="3" /><ContentPresenter Content="省名" Grid.Row="2" VerticalAlignment="Center" HorizontalAlignment="Center" /><Rectangle Fill="#ccc" VerticalAlignment="Stretch" Width="1" Visibility="Visible" Grid.Row="2" Grid.Column="1" /><ContentPresenter Content="城市" Grid.Row="2" Grid.Column="2" VerticalAlignment="Center" HorizontalAlignment="Center" /></Grid><Rectangle x:Name="VerticalSeparator" Fill="#ccc" VerticalAlignment="Stretch" Width="1" Visibility="Visible" Grid.Row="1" Grid.Column="1" /></Grid></ControlTemplate></Setter.Value></Setter></Style></Window.Resources><Grid><DataGrid AutoGenerateColumns="False" HorizontalAlignment="Stretch" Name="dataGrid1" VerticalAlignment="Stretch"><DataGrid.Columns><DataGridTemplateColumn Header="sec" HeaderStyle="{StaticResource CityNameStyle}"><DataGridTemplateColumn.CellTemplate><DataTemplate><StackPanel Orientation="Horizontal"><Label Content="{Binding AddrName}" Width="100" /><Rectangle Fill="#000" VerticalAlignment="Stretch" Margin="0" Width="1" /><Label Content="{Binding CityName}" Width="100" /></StackPanel></DataTemplate></DataGridTemplateColumn.CellTemplate></DataGridTemplateColumn><DataGridTextColumn HeaderStyle="{StaticResource CityNumStyle}" Binding="{Binding TelNum}" Width="100" /><DataGridTextColumn Header="统计" Binding="{Binding TotalSum}" Width="100" /></DataGrid.Columns></DataGrid></Grid>&nbsp;/// <summary>/// MainWindow.xaml 的交互逻辑/// </summary>public partial class MainWindow : Window{public MainWindow(){InitializeComponent();this.dataGrid1.ItemsSource = CityInformation.GetInfo();}}public class CityInformation{public string AddrName { get; set; }public string CityName { get; set; }public string TelNum { get; set; }public double TotalSum { get; set; }public static List<CityInformation> GetInfo(){return new List<CityInformation>()&nbsp;{new CityInformation() { AddrName="四川", CityName = "成都", TelNum="123", TotalSum = 1.23 },new CityInformation() { AddrName="广东", CityName = "广州", TelNum="234", TotalSum = 1.23 },new CityInformation() { AddrName="广西", CityName = "南宁", TelNum="0152", TotalSum = 1.23 },new CityInformation() { AddrName="贵州", CityName = "贵阳", TelNum="0123", TotalSum = 1.23 },new CityInformation() { AddrName="四川", CityName = "成都", TelNum="123", TotalSum = 10.23 }};}}

万千封印

用模板行不,放个label什么的,再放个dataGrid,这样应该可以实现,就是不知道效果怎么样
随时随地看视频慕课网APP
我要回答