简体   繁体   English

WPF - 更改数据绑定 DataGrid 上的列名

[英]WPF - Changing Column Name on Data Bound DataGrid

Basically I'm using the ItemSource property of the datagrid to bind a generic list to my datagrid.基本上,我使用数据网格的 ItemSource 属性将通用列表绑定到我的数据网格。 However I'd really like to change the headings, I tried the following but I get a runtime exception:但是我真的很想更改标题,我尝试了以下操作,但出现运行时异常:

dgtest.Columns[1].Header = "edited";

I used the AutoGeneratingColumn event and an Attribute to set my column names.我使用 AutoGeneratingColumn 事件和一个属性来设置我的列名。

First create an attribute class...首先创建一个属性类...

    public class ColumnNameAttribute : System.Attribute
    {
        public ColumnNameAttribute(string Name) { this.Name = Name; }
        public string Name { get; set; }
    }

Then I decorate my data class members with the new attribute...然后我用新属性装饰我的数据类成员......

    public class Test
    {
        [ColumnName("User Name")]
        public string Name { get; set; }
        [ColumnName("User Id")]
        public string UserID { get; set; }
    }

Then I write my AutoGeneratingColumn event handler...然后我编写我的 AutoGeneratingColumn 事件处理程序......

    void dgPrimaryGrid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
    {
        var desc = e.PropertyDescriptor as PropertyDescriptor;
        var att = desc.Attributes[typeof(ColumnNameAttribute)] as ColumnNameAttribute;
        if(att != null)
        {
            e.Column.Header = att.Name;
        }
    }

... and attach it to my grid and test... ...并将其附加到我的网格并测试...

        dgPrimaryGrid.AutoGeneratingColumn += dgPrimaryGrid_AutoGeneratingColumn;

        var data = new object[] 
        {
            new Test() { Name = "Joe", UserID = "1" }
        };
        dgPrimaryGrid.ItemsSource = data;

Here is what it looks like.这是它的样子。 Notice that the column names are not the property names (the default behavior).请注意,列名称不是属性名称(默认行为)。

列重命名的 DataGrid

This approach is a little more work, but it's nice to have the column heading defined at the same place as the bound column.这种方法需要更多的工作,但是在绑定列的同一位置定义列标题会很好。 You can reorder your columns without having to go to other places to fix c the column names.您可以重新排序您的列,而无需去其他地方修复列名。

You can change it on the ItemDataBound event:您可以在ItemDataBound事件上更改它:

public void yourDataGrid_OnItemDataBound(object s, DataGridItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Header)
    {
        // Change the cell index to the column index you want... I just used 0
        e.Item.Cells[0].Text = "Text you want in header.";
    }
}

If the grid is already bound you should be able to do:如果网格已经绑定,您应该能够执行以下操作:

yourDataGrid.Columns[0].Header = "Text you want in header.";

You are probably getting an error because you are trying to change the text before it is bound.您可能会收到错误消息,因为您试图在绑定之前更改文本。

AutoGeneratedColumns event on wpf for change column name wpf 上的 AutoGeneratedColumns 事件用于更改列名

datagrid1.AutoGeneratedColumns += datagrid1_AutoGeneratedColumns;

void datagrid1_AutoGeneratedColumns(object sender, EventArgs e)
{
    datagrid1.Columns[0].Header = "New Column Name";
}

1) Switch off the automatic column generation and generate your data grid columns in the program code: 1)关闭自动列生成并在程序代码中生成数据网格列:

DataGridTextColumn TempColumn;    

MyDataGrid.AutoGenerateColumns = false;

TempColumn = new DataGridTextColumn();
TempColumn.Header = "DisplayName0";
TempColumn.Binding = new Binding("BindingName0");
MyDataGrid.Columns.Add(TempColumn);

TempColumn = new DataGridTextColumn();
TempColumn.Header = "DisplayName1";
TempColumn.Binding = new Binding("BindingName1");
MyDataGrid.Columns.Add(TempColumn);

Then "BindigName0" is the internal binding name of column 0 and "DisplayName0" is the name that the user will see.然后“BindigName0”是第 0 列的内部绑定名称,“DisplayName0”是用户将看到的名称。

2) If you want to use the automatic column generation instead then the display names of the columns can be set in the "AutoGeneratingColumn" event: 2)如果要使用自动列生成,则可以在“AutoGeneratingColumn”事件中设置列的显示名称:

MyDataGrid.AutoGeneratingColumn += MyDataGrid_AutoGeneratingColumn;

...

private void MyDataGrid_AutoGeneratingColumn(object sender, 
              DataGridAutoGeneratingColumnEventArgs e)
{
  DataGridBoundColumn TempColumn;
  string BindingName;

  if (e.Column is DataGridBoundColumn)
  {
    TempColumn = e.Column as DataGridBoundColumn;
    BindingName = (TempColumn.Binding as Binding).Path.Path;
    if (BindingName == "BindingName0")
    {
      TempColumn.Header = "DisplayName0";
    }
    else if (BindingName == "BindingName1")
    {
      TempColumn.Header = "DisplayName1";
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM