WPF的datagrid 如何用2种颜色交替做每行背景颜色?

qq_23313131 2014-12-04 02:09:45
试过用
var row = dataGrid.ItemContainerGenerator.ContainerFromItem(dataGrid.Items[i]) as DataGridRow;


但每次取到的对象都为null
...全文
1177 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
wswxfwps 2016-07-07
  • 打赏
  • 举报
回复
如果在项目中把创建数据源、绑定数据源、对datagrid进行操作(改变行的颜色、高度)都写在一个事件中,其中在取datagrid的row时出现错误:未将对象引用设置到对象的实例。 解决的方法: dataGrid1.UpdateLayout();
  • 打赏
  • 举报
回复
构造函数:

this.dgList.LoadingRow += new EventHandler<DataGridRowEventArgs>(dgList_LoadingRow);

 void dgList_LoadingRow(object sender, DataGridRowEventArgs e)
        {
            int index = e.Row.GetIndex();
            if (index % 2 == 0)
            {
                e.Row.Background = Brushes.Gray;  // color
            }
            else {
                e.Row.Background = Brushes.Yellow;
            }
            //e.Row.Header = index + 1;
        }
_Monkey_King 2014-12-04
  • 打赏
  • 举报
回复
用repeater吧= =想怎么样就怎么样
风的期待 2014-12-04
  • 打赏
  • 举报
回复
如果我要在指定的单元格选择指定的北京颜色怎么做啊?
静静-风 2014-12-04
  • 打赏
  • 举报
回复
//ListItemType.Item为奇数行 //ListItemType.AlternatingItem为偶数行 switch (e.Item.ItemType) { case ListItemType.AlternatingItem: e.Item.BackColor= break; case ListItemType.Item: e.Item.BackColor= break; }
Forty2 2014-12-04
  • 打赏
  • 举报
回复
在Xaml里面设置一下就好了: <DataGrid AlternationCount="2" AlternatingRowBackground="PeachPuff" Name="myDataGrid" />

111,098

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧