wpf 使用 DataGrid 在一列中显示多个标题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16696755/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Showing multiple headers in one column with DataGrid
提问by hattenn
I would like to have more than one property in a column like the following:
我想在一列中拥有多个属性,如下所示:


Now, it's easy to create a cell template to show more than one property, but how would one go about creating a header template that shows more than one property that still lets you sort by clicking on them? So you should be able to sort by first namejust by clicking on First Nameheader, and same goes for all the other properties.
现在,创建一个单元格模板来显示多个属性很容易,但是如何创建一个显示多个属性的标题模板,仍然允许您通过单击它们进行排序?因此,您应该能够通过单击标题按名字排序First Name,所有其他属性也是如此。
采纳答案by kmatyaszek
You can use two TextBlock. In Tagproperty you should transfer name of the property from your data class. This string from Tagproperty you will use to set SortMemberPath. In the event MouseLeftButtonDownyou can get from Tagproperty name of the actual sort property and assign it to SortMemberPath.
您可以使用两个TextBlock. 在Tag属性中,您应该从数据类中传输属性的名称。Tag您将用于设置的属性中的此字符串SortMemberPath。如果MouseLeftButtonDown您可以从Tag实际排序属性的属性名称中获取并将其分配给SortMemberPath.
<DataGrid Name="dataGrid1" ItemsSource="{Binding}" AutoGenerateColumns="False" Margin="0,0,0,52">
<DataGrid.Columns>
<DataGridTemplateColumn>
<DataGridTemplateColumn.HeaderTemplate >
<DataTemplate>
<Grid ShowGridLines="True">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<TextBlock Text="First Name" Tag="FirstName" MouseLeftButtonDown="TextBlock_MouseLeftButtonDown" />
<TextBlock Text="Last Name" Grid.Row="1" Tag="LastName" MouseLeftButtonDown="TextBlock_MouseLeftButtonDown" />
</Grid>
</DataTemplate>
</DataGridTemplateColumn.HeaderTemplate>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<TextBox Text="{Binding FirstName}"/>
<TextBox Text="{Binding LastName}" Grid.Row="1" />
</Grid>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Header="Age" Binding="{Binding Age}" />
</DataGrid.Columns>
</DataGrid>
Code-behind:
代码隐藏:
private void TextBlock_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
TextBlock s = sender as TextBlock;
string sortPath = s.Tag as string;
dataGrid1.Columns[0].SortMemberPath = sortPath;
}
You can also add TextBlockstyle if you want to show which property is currently sorting property and bold it font.
TextBlock如果您想显示当前正在排序的属性并将其字体加粗,您还可以添加样式。
<DataGridTemplateColumn.HeaderTemplate >
<DataTemplate>
<Grid ShowGridLines="True">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<TextBlock Text="First Name" Tag="FirstName" MouseLeftButtonDown="TextBlock_MouseLeftButtonDown">
<TextBlock.Style>
<Style TargetType="TextBlock">
<Setter Property="FontWeight" Value="Normal" />
<Style.Triggers>
<DataTrigger Binding="{Binding Path=Columns[0].SortMemberPath, ElementName=dataGrid1}">
<DataTrigger.Value>
<sys:String>FirstName</sys:String>
</DataTrigger.Value>
<Setter Property="FontWeight" Value="Bold" />
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
<TextBlock Text="Last Name" Grid.Row="1" Tag="LastName" MouseLeftButtonDown="TextBlock_MouseLeftButtonDown" >
<TextBlock.Style>
<Style TargetType="TextBlock">
<Setter Property="FontWeight" Value="Normal" />
<Style.Triggers>
<DataTrigger Binding="{Binding Path=Columns[0].SortMemberPath, ElementName=dataGrid1}">
<DataTrigger.Value>
<sys:String>LastName</sys:String>
</DataTrigger.Value>
<Setter Property="FontWeight" Value="Bold" />
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
</Grid>
</DataTemplate>
</DataGridTemplateColumn.HeaderTemplate>
Where sysis following namespace:
sys以下命名空间在哪里:
xmlns:sys="clr-namespace:System;assembly=mscorlib"
回答by Christopher Sauer
Take a look at this example of StringFormat:
And change the header of your DataGrid column like the example below :
并像下面的示例一样更改 DataGrid 列的标题:
<DataGrid>
<DataGrid.Columns>
<DataGridTemplateColumn>
<DataGridTemplateColumn.Header>
<TextBlock HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<TextBlock.Text>
<MultiBinding StringFormat="{}{1}{0}{2}">
<Binding Source="{x:Static sys:Environment.NewLine}"/>
<Binding Path="FirstName" />
<Binding Path="LastName"/>
</MultiBinding>
</TextBlock>
</DataGridTemplateColumn.Header>
</DataGrid.Columns>
</DataGrid>
with xmlns:sys="clr-namespace:System;assembly=mscorlib"
和 xmlns:sys="clr-namespace:System;assembly=mscorlib"

