0

我想向 HeaderRow 添加另一行,并为每一列插入特定的控件。
这是我添加这一行的代码,但我只能为每一列设置相同的控件(在本例中为文本框),但我想为特定列设置特定控件。(就像在 ASP.NET 中继器中完成的那样)

<Style x:Key="DataGridColumnHeaderStyle1" TargetType="{x:Type DataGridColumnHeader}">
   <Setter Property="Template">
      <Setter.Value>
         <ControlTemplate TargetType="{x:Type DataGridColumnHeader}">
           <Grid x:Name="grid" VerticalAlignment="Center" HorizontalAlignment="Stretch"  Background="{TemplateBinding Background}">
              <Grid.RowDefinitions>
                <RowDefinition Height="*"/>
                <RowDefinition Height="*"/>
              </Grid.RowDefinitions>
              <TextBlock Name="ColumnHeader" Grid.Row="0" Text="{TemplateBinding Content}"  HorizontalAlignment="Center" VerticalAlignment="Center" Height="18" />
              <Grid Grid.Row="1">
                 <TextBox Text="" HorizontalAlignment="Stretch" BorderThickness="0,1,1,1" />
              </Grid>
          </Grid>
</Style>
4

2 回答 2

4

您可以使用 HeaderTemplate 为每个 Column 指定不同的数据模板

样本

<Window.Resources>
    <DataTemplate x:Key="Template1">
        <Grid x:Name="grid" VerticalAlignment="Center" HorizontalAlignment="Stretch">
            <Grid.RowDefinitions>
                <RowDefinition Height="*"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
            <TextBlock Name="ColumnHeader" Text="Student Id" Grid.Row="0"  HorizontalAlignment="Center" VerticalAlignment="Center" Height="18" />
            <Grid Grid.Row="1">
                <TextBox Text="" HorizontalAlignment="Stretch" BorderThickness="0,1,1,1" />
            </Grid>
        </Grid>
    </DataTemplate>
    <DataTemplate x:Key="Template2">
        <Grid x:Name="grid" VerticalAlignment="Center" HorizontalAlignment="Stretch">
            <Grid.RowDefinitions>
                <RowDefinition Height="*"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
            <TextBlock Name="ColumnHeader" Text="Student Name" Grid.Row="0"  HorizontalAlignment="Center" VerticalAlignment="Center" Height="18" />
            <Grid Grid.Row="1">
                <TextBox Text="" HorizontalAlignment="Stretch" BorderThickness="0,1,1,1" />
            </Grid>
        </Grid>
    </DataTemplate>
</Window.Resources>
<Grid>
    <DataGrid Name="dataGrid" >
        <DataGrid.Columns>
            <DataGridTextColumn HeaderTemplate="{StaticResource Template1}"  Width="90*" MinWidth="120" Binding="{Binding StudentId}"/>
            <DataGridTextColumn HeaderTemplate="{StaticResource Template2}" Width="90*" MinWidth="120" Binding="{Binding Name}"/>
            <DataGridTextColumn Header="Score" Width="100*" MinWidth="150" Binding="{Binding Score}"/>
        </DataGrid.Columns>
    </DataGrid>

</Grid>
于 2011-05-20T09:16:34.563 回答
3

我对此有点不同,我设置了 DataGridColumnHeadersPresenter 的样式,它允许我设置列标题行(而不是列标题)。
在那里我添加了我的新行和我需要的所有控件,我还设置了每个单元格的宽度以匹配它的 header 的宽度。

<Style x:Key="DataGridColumnHeadersPresenterStyle1" TargetType="{x:Type DataGridColumnHeadersPresenter}">
    <Setter Property="Template">
      <Setter.Value>
          <ControlTemplate TargetType="{x:Type DataGridColumnHeadersPresenter}">
              <Grid>
                <Grid.RowDefinitions>
                  <RowDefinition Height="*"/>
                  <RowDefinition Height="*"/>                   
              </Grid.RowDefinitions>
              <DataGridColumnHeader x:Name="PART_FillerColumnHeader" IsHitTestVisible="False"/>                         
              <ItemsPresenter/>
              <Grid  Grid.Row="1" VerticalAlignment="Top" HorizontalAlignment="Left" >

                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="{Binding ActualWidth, ElementName=Col1}"/>         
                    <ColumnDefinition Width="{Binding ActualWidth, ElementName=Col2}"/>
                    <ColumnDefinition Width="{Binding ActualWidth, ElementName=Col3}"/>
                </Grid.ColumnDefinitions>
                <TextBox  Grid.Column="0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Bottom" Width="Auto" Height="25" BorderThickness="1,2,0,2"/>
                   my other controls go here
              </Grid>
            </Grid>
        </ControlTemplate>
    </Setter.Value>
 </Setter>

于 2011-05-23T11:48:11.110 回答