1. 程式人生 > >WPF DataGrid 實現行顏色改變

WPF DataGrid 實現行顏色改變

1、單行顏色的改變

   按照以前WinForm的思想,應該是直接獲得單行的物件,然後設定BackGround即可,但是在WPF 中DataGrid是不能直接獲得

DataGridRow 的物件的,所以要使用LoadingRow事件來獲得DataGridRow 的物件,程式碼如下:

 private void dataGrid_LoadingRow(object sender, Microsoft.Windows.Controls.DataGridRowEventArgs e)
        {
            DataGridRow dataGridRow = e.Row;
            CommandResultInfo dataRow = e.Row.Item as CommandResultInfo;
            if (某行某列的值==某值)
            {
                dataGridRow.Background = Brushes.Plum;
            }
        }

2、隔行顏色改變

    可以使用DataGrid類中的屬性來實現,屬性的名稱為: AlternationCount="2"