5

我想在一列中拥有多个属性,如下所示:

一列中有多个标题的 DataGrid。

现在,创建一个显示多个属性的单元格模板很容易,但是如何创建一个显示多个属性的标题模板,仍然允许您通过单击它们进行排序?因此,您应该能够通过单击标题按名字First Name排序,所有其他属性也是如此。

4

2 回答 2

4

您可以使用两个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"
于 2013-05-22T17:09:43.127 回答
0

看看这个 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"

于 2014-01-30T10:54:42.843 回答