【问题标题】:DataGrid Row Header WPF数据网格行标题 WPF
【发布时间】:2016-05-19 04:55:03
【问题描述】:

我有一个包含 7 个已定义列的填充数据表 (xDataTable) - 我想用作我的 RowHeader 的第一列。

我也有一个 DataGrid:

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

然后我设置我的 DataGrid 的 DataContext:

DataGridX.DataContext = xDataTable;

这一切都有效 - 但如何将 DataGrid 的第一列设置为 RowHeader?

【问题讨论】:

  • 是 MVVM 还是代码隐藏?
  • 你是自动生成列吗?
  • @KyloRen 我的列是使用 .Columns.Add() 硬编码的。
  • @JoelH 然后不要在数据网格中添加该列并将该列绑定到行标题。

标签: c# wpf datagrid datatable


【解决方案1】:

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

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

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

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

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

【讨论】:

  • 即使是第二个例子,行标题总是等于第一列的名称。也许我没有完全清楚地表达我的答案 - 我在第一列中的值是行标题之后。
  • 好的,我明白了。如果您没有自动生成列,那么只需将标题与该列名绑定(就像现在在我的第二个示例中一样)并从列定义中删除 Datagrid 列。 @JoelH 应该可以工作
  • 这行得通——我的第一列现在是行标题。但是,然后第一列再次显示为另一列 - 我可以阻止这种情况吗?
  • 是的。您不需要在网格中添加该列。在做 Columns.Add().
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-03-21
    • 1970-01-01
    • 2015-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-11
    • 2010-12-12
    相关资源
    最近更新 更多