WPF中的DataGrid行标题

8

我有一个已经填充好数据的 DataTable(xDataTable),其中包含7个定义好的列 - 我想要将第一列作为我的行标题。

另外我还有一个 DataGrid:

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

然后我设置了我的DataGrid的DataContext:

DataGridX.DataContext = xDataTable;

这一切都可以实现 - 但是我如何将我的DataGrid的第一列设置为行标题?


是 MVVM 还是 Code-Behind? - StepUp
你正在自动生成列吗? - Kylo Ren
@KyloRen 我的列是使用.Columns.Add()硬编码的。 - JoelH
@JoelH 那就不要在数据网格中添加该列,并将该列绑定到行标题。 - Kylo Ren
2个回答

8

请使用以下样式(通常情况下):

<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>

如果我们想要为不同的行使用单独的行标题,则可以使用以下方法:

<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"/>

然后移除这一列并使用此绑定作为表头。

即使是第二个示例,行标题始终等于第一列的名称。也许我没有完全表达我的答案 - 我想要的是第一列中的值成为行标题。 - JoelH
好的,我明白了。如果您没有自动生成列,则只需将标题与该列名称绑定(如现在在我的第二个示例中),并从列定义中删除Datagrid列即可。@JoelH 那应该可以工作。 - Kylo Ren
2
那很有效 - 我的第一列现在是行标题。然而,第一列又显示为另一列 - 我能防止这种情况吗? - JoelH
是的,您不需要在网格中添加该列。在执行Columns.Add()时。 - Kylo Ren

0

您可以设置任何您想要的标题。只需像这样添加您的列:

 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();
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接