数据网格行标题 WPF

本文关键字:标题 WPF 网格 数据网 数据 | 更新日期: 2023-09-27 18:33:44

我有一个填充的DataTable (xDataTable),其中包含7个定义的列 - 我想将第一列用作我的RowHeader。

我还有一个数据网格:

 <DataGrid x:Name="DataGridX" ItemsSource="{Binding}" Grid.Row="0" 
           CanUserAddRows="False" SelectionUnit="Cell" />

然后,我设置了 DataGrid 的 DataContext:

DataGridX.DataContext = xDataTable;

这一切都有效 - 但是如何将数据网格的第一列设置为行头?

数据网格行标题 WPF

使用以下样式(通常大小写):

<DataGrid.RowHeaderStyle>
    <Style TargetType="{x:Type DataGridRowHeader}">
        <Setter Property="Content" Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGrid}},Path=Columns[0].Header,Mode=OneTime}" />
    </Style>
</DataGrid.RowHeaderStyle>

如果我们想要单独的 RowHead 用于单独的 RowHead,请使用:

<DataGrid.RowStyle>
    <Style TargetType="{x:Type DataGridRow}">
        <Setter Property="IsSelected" Value="{Binding IsRowSelected}" />
        <Setter Property="Header" Value="{Binding Content}" />
    </Style>
</DataGrid.RowStyle>

只需根据需要更改上述绑定即可。

如果第一列为:

<DataGridTextColumn Binding="{Binding Content}" Header="Content"/>

然后删除此列并将此绑定用于标头。

您可以根据需要设置任何标头。只需像这样添加列:

 DataTable.Columns.Add("I am a Column Header!:)");

让我们看看 MVVM 示例:

public class YourViewModel : ViewModelBase
{
    public YourViewModel()
    {
        PopulateDataTable();
    }
    private void PopulateDataTable()
    {
        var _ds = new DataSet("Test");
        employeeDataTable = new DataTable();
        employeeDataTable = _ds.Tables.Add("DT");
        for (int i = 0; i < 20; i++)
        {                
            //you can set any Header in the following line
            employeeDataTable.Columns.Add(i.ToString());
        }
        for (int i = 0; i < 10; i++)
        {
            var theRow = employeeDataTable.NewRow();
            for (int j = 0; j < 20; j++)
            {
                theRow[j] = "a";                    
            }
            employeeDataTable.Rows.Add(theRow);
        }
    }
     private DataTable employeeDataTable;
     public DataTable EmployeeDataTable
     {
        get { return employeeDataTable; }
        set
        {
            employeeDataTable = value;
            OnPropertyChanged("EmployeeDataTable");
        }
     }
}

您的 XAML:

<DataGrid ItemsSource="{Binding EmployeeDataTable}" />

更新:

让我们看一下代码隐藏示例:

您的 XAML:

<DataGrid Name="dataGrid"/>

您的代码隐藏:

//constructor of the Window
public MainWindow()
{
    InitializeComponent();
    PopulateDataGrid();
}
DataTable employeeDataTable = new DataTable();

private void PopulateDataGrid()
{
   var _ds = new DataSet("Test");
   employeeDataTable = _ds.Tables.Add("DT");
   for (int i = 0; i < 10; i++)//create columns
   {   
        employeeDataTable.Columns.Add("I am a column!:)");
   }
   for (int i = 0; i < 50; i++)//fill data to rows
   {
       var theRow = employeeDataTable.NewRow();
       for (int j = 0; j < 10; j++)
       {
          if (j % 2 == 0)                    
              theRow[j] = "a";  
          else                  
              theRow[j] = "b";
       }
       employeeDataTable.Rows.Add(theRow);
   }
   dataGrid.ItemsSource = employeeDataTable.AsDataView();
}