WPF - 对数据绑定DataGrid中更改列名 [英] WPF - Changing Column Name on Data Bound DataGrid

查看:1457
本文介绍了WPF - 对数据绑定DataGrid中更改列名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上我使用DataGrid的财产的ItemSource绑定到一个通用的列表,我的数据网格。不过我真的想改标题,我尝试以下,但我得到一个运行时异常:

  dgtest.Columns [ 1] .Header =编辑; 


解决方案

您可以改变它的的ItemDataBound 事件:

 公共无效yourDataGrid_OnItemDataBound(对象S,DataGridItemEventArgs E)
{
如果(e.Item.ItemType == ListItemType.Header)
{
//改变细胞指数到你想要的列索引...我只是用0
é .Item.Cells [0]。文本=TEXT你想的头。
}
}

如果电网已经被绑定,你应该能够这样做:

  yourDataGrid.Columns [0] .Header =文本要在标题; 

您可能正在得到,因为你正在试图改变文本势必前一个错误。


Basically I'm using the ItemSource property of the datagrid to bind a generic list to my datagrid. However I'd really like to change the headings, I tried the following but I get a runtime exception:

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

解决方案

You can change it on the ItemDataBound event:

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.

这篇关于WPF - 对数据绑定DataGrid中更改列名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆