为了账号安全,请及时绑定邮箱和手机立即绑定

WPF的DataGrid怎么实现多行表头

WPF的DataGrid怎么实现多行表头

慕容3067478 2018-08-10 08:06:52
所谓的中国式的表头             2011年1月 2月 3月 。。。。。这样的
查看完整描述

2 回答

?
撒科打诨

TA贡献1934条经验 获得超2个赞

<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>

 

/// <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>() 
{
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 }
};
}
}


查看完整回答
反对 回复 2018-08-13
?
MMTTMM

TA贡献1869条经验 获得超4个赞

用模板行不,放个label什么的,再放个dataGrid,这样应该可以实现,就是不知道效果怎么样,呵呵

查看完整回答
反对 回复 2018-08-13
  • 2 回答
  • 0 关注
  • 1574 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信