我希望列中包含多个属性,如下所示:
现在,创建一个单元格模板以显示多个属性很容易,但是如何创建一个显示多个属性的标题模板仍然允许您通过单击它们进行排序?因此,您只需点击First Name
标题即可按名字进行排序,其他属性也是如此。
答案 0 :(得分:3)
您可以使用两个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>
代码隐藏:
private void TextBlock_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
TextBlock s = sender as TextBlock;
string sortPath = s.Tag as string;
dataGrid1.Columns[0].SortMemberPath = sortPath;
}
如果要显示当前正在排序属性的属性并加粗字体,也可以添加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>
sys
遵循命名空间:
xmlns:sys="clr-namespace:System;assembly=mscorlib"
答案 1 :(得分:0)
看看this example of StringFormat:
并更改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>
xmlns:sys="clr-namespace:System;assembly=mscorlib"