<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 }
};
}
}
用模板行不,放个label什么的,再放个dataGrid,这样应该可以实现,就是不知道效果怎么样,呵呵
WPF里面好像没有table
@beyondkko: label
只有自己绘制了
如果真要这么干的话可要小心啊 你需要修改datagridcolumnheader的 contenttemplate 然后在contenttemplate中你在加入 你的<DataTemplate> <Grid> 内容</Grid></DataTemplate> 然后改成你要的那种 不过你这个列必须得是TemplateColumn 然后你还得 在TemplateColumn 中的 CellTemplate中 在添加一个datagrid或者是用listbox 然后再listbox的itemTemplate中在做了 缺点是 不能改变单独的一行的 宽度啊 而且 wpf中的datagrid 的格子数量比较大的时候 由于有Virtual 所以 你在滚动的时候的performance 的性能还会有所下降 这个属于wpf的一个硬伤