Silverlight uses the DataGrid row loading event to dynamically control row and column display, silverlightdatagrid
The datagrid binding is very useful, but sometimes we often need to dynamically control rows or columns based on the model content. What should we do?
At this time, we need to use the row loading event: when loading each row of data, we can control the corresponding table display based on the data content.
For example, if we want to set the baseline color of each row to red, we can do this:
SolidColorBrush r = new SolidColorBrush(Colors.Red); private void dataGrid1_LoadingRow(object sender, DataGridRowEventArgs e) { CData c = e.Row.DataContext as CData; if (c.id % 5 == 0) e.Row.Background = r; else e.Row.Background = new SolidColorBrush(Colors.White); }
If you want to change the style or content of a custom column in a row based on the value of the model, you can also do the following:
Private void dg_source2_LoadingRow (object sender, DataGridRowEventArgs e) {db_lsyjbInfo bindData = (db_lsyjbInfo) e. row. dataContext; TextBox btn = dg_source2.Columns [0]. getCellContent (e. row ). findName ("tb") as TextBox; // locate the if (bindData. zbbm = "xjl") btn. isReadOnly = true ;}
Just put, row loading is very useful, but when we control each row of a table or a child control of a column in a row, we need to do a good job of positioning, you need to work hard to locate the control at ordinary times!
For example:
DataGridCell aa = dg_source2.Columns [0]. GetCellContent (e. Row) as DataGridCell; // locate a specific cell (standard column)
Here, we will learn and accumulate.