如何在wpf中更改datagrid的单细胞颜色? [英] How to change single cell color of datagrid in wpf?

查看:329
本文介绍了如何在wpf中更改datagrid的单细胞颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我想在运行时更改此网格的特定单元格的背景颜色(显示已预订的座位)。我在窗口加载的事件上绑定了这个网格从datatable。我有一个座位记录,如'A33'。绑定的代码是这样的。

I want to change background color of a particular cell of this grid at runtime(show booked seats).i am binding this grid from datatable on window loaded event.i have a record of seats like 'A33'.my code for binding is like this.

MySqlConnection mycon = new MySqlConnection(str);
mycon.Open();
MySqlDataAdapter da = new MySqlDataAdapter("select * from Stage", mycon);
da.Fill(dt);
MyGrid.ItemsSource = dt.DefaultView;


推荐答案

您的单元格数据应该有一个属性 IsBooked ,然后在 DataGrid.CellStyle 中,您可以使用 IsBooked 改变它的背景。 (除了 DataTriggers 之外,还有其他一些选项,但如果您只是有一个布尔条件我发现他们很方便。)

Your cell-data should have a property IsBooked, then in the DataGrid.CellStyle you can use a data-trigger on IsBooked to change its backgroud. (There are some other alternatives besides DataTriggers, but if you just have one boolean conditional i find them to be quite convenient.)

这篇关于如何在wpf中更改datagrid的单细胞颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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