繁体   English   中英

WPF DataGrid的行颜色没有发生改变

[英]wpf datagrid row color changing not happening

我正在比较从WPF Datagrid行项目到数据库项目的行值,找到匹配项后,我需要更改Datagrid中特定行的颜色。 直到找到比赛都可以

  foreach (System.Data.DataRowView rowview in dataGrid1.Items )
  {
    var Srow = dataGrid1.ItemContainerGenerator.ContainerFromItem(dataGrid1.SelectedItem) as DataGridRow;
    Srow.Background = Brushes.LightGreen;
  }

给错误(未处理的空引用。)我尝试了所有选项保持索引,rowview.Row [“ Name”] .....

欢迎任何建议。

嗨,我建议您实施LoadingRow事件,因为更改行样式将更加容易:

  //The event: 
   dataGrid1.LoadingRow += new EventHandler<DataGridRowEventArgs>(dataGrid1_LoadingRow);

现在处理事件的函数

  //Here come the function
   private void dataGrid1_LoadingRow(object sender, DataGridRowEventArgs e)
   {
     //your test here to see if they match...
       DataRowView row = (DataRowView)e.Row.Item;
       String Content = row.Row[i].ToString();
       if(Content = //Did it matche something ?)
       { 
         //If yes:
          e.Row.Background = new SolidColorBrush(Color.FromArgb(255, 217, 77, 77));
         //or 
         e.Row.Background = Brushes.LightGreen;
       }
   } 

就是这样,让我知道它是否对您有用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM