DataGrid 通过行内容动态改变背景色

示例代码:

效果:

1.XAML部分

<Window x:Class="WpfApp2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp2"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>

</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition Height="40"/>
</Grid.RowDefinitions>
<DataGrid x:Name="ErrList" AlternationCount="3" AutoGenerateColumns="False" >
<DataGrid.RowStyle>
<Style TargetType="{x:Type DataGridRow}">
<Style.Triggers>
<Trigger Property="AlternationIndex" Value="0">
<Setter Property="Background" Value="Gray"/>
</Trigger>
<Trigger Property="AlternationIndex" Value="1">
<Setter Property="Background" Value="#ff22dd"/>
</Trigger>
</Style.Triggers>
</Style>
</DataGrid.RowStyle>
<DataGrid.Columns>
<DataGridTextColumn
Width="Auto"
MinWidth="50"
Binding="{Binding No}"
Header="编号"
/>
<DataGridTextColumn
Width="400"
MinWidth="250"
Binding="{Binding Content}"
Header="故障描述"
/>
</DataGrid.Columns>
</DataGrid>
<StackPanel Orientation="Horizontal" Grid.Row="1">
<Button Content="更新" Width="100" Margin="3" Click="Button_Click"/>
<Button Content="设置故障" Width="100" Margin="3" Click="Button_Click_1"/>
</StackPanel>
</Grid>
</Window>

2.CS部分

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WpfApp2.Model;
using System.Collections.ObjectModel;
using System.Data;

namespace WpfApp2
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
private ObservableCollection<Error> errorList;
public MainWindow()
{
InitializeComponent();
initData();
}
private void initData() {
errorList = new ObservableCollection<Error> {
new Error{No=1,Content="受电弓故障",Occ=false },
new Error{ No=2,Content="主断合不上处理",Occ=false},
new Error{ No=3,Content="主断合不上处理",Occ=false},
new Error{ No=4,Content="主断合不上处理",Occ=false},
new Error{ No=5,Content="主断合不上处理",Occ=false},
new Error{ No=6,Content="主断合不上处理",Occ=false},
new Error{ No=7,Content="主断合不上处理",Occ=false},
};
this.ErrList.ItemsSource = errorList;
}

private void Button_Click(object sender, RoutedEventArgs e)
{
Error obj = errorList[3];
if (obj.Occ)
obj.Occ = false;
else
obj.Occ = true;
}

private void Button_Click_1(object sender, RoutedEventArgs e)
{
for (int i = 0; i < this.errorList.Count; i++)
{
Error drv = errorList[i] as Error;
var row = ErrList.ItemContainerGenerator.ContainerFromItem(drv) as DataGridRow;
if (drv.Occ)
row.Background = Brushes.White;
else
row.Background = Brushes.Gray;

}
}
}
}

3.实现类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WpfApp2.Model
{
public class Error:BaseNotify
{
private int no;
private string content;
private bool isocc;
public int No { get { return no; }
set {
no = value;
RaiseProperty("No");
} }
public string Content { get { return content; }
set {
content = value;
RaiseProperty("Content");
}
}
public bool Occ { get { return isocc; }
set {
isocc = value;
RaiseProperty("Occ");
}
}
}
}

4.基类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;
namespace WpfApp2.Model
{
public class BaseNotify : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void RaiseProperty(string PropertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this,new PropertyChangedEventArgs(PropertyName));
}
}

}
}

猜你喜欢

转载自www.cnblogs.com/sundh1981/p/13388291.html