问题描述
我想在运行时更改此网格的特定单元格的背景颜色(显示已预订的座位)。我在窗口加载的事件上绑定了这个网格从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的单细胞颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!