1. 程式人生 > >C# WPF DataGrid 根據某列的值設定行的背景色

C# WPF DataGrid 根據某列的值設定行的背景色

最簡單的方法是 使用 datagrid的LoadingRow事件。
     private void datagrid_LoadingRow(object sender, DataGridRowEventArgs e)
        {
            var drv = e.Row.Item as DataRowView;
            switch (drv["ID"].ToString())
            {
                case "1": e.Row.Background = new SolidColorBrush(Colors.Green);
                    break;
                case "2": e.Row.Background = new SolidColorBrush(Colors.Yellow);
                    break;
                case "3": e.Row.Background = new SolidColorBrush(Colors.CadetBlue);
                    break;


            }   
        }