You can achieve this behaviour by a so-called value converter:
This is just an example of how you could set colours
depending on values.
ValueToColorConverter.cs
using System;
using System.Windows.Data;
using System.Windows.Media;
namespace WpfApplication12
{
class ValueToColorConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if(value.GetType() == typeof(bool))
{
if((bool)value)
{
return Brushes.Green;
}
else
{
return Brushes.Red;
}
}
else
{
return value;
}
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
MainWindow.xaml:
<Window x:Class="WpfApplication12.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication12"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<local:ValueToColorConverter x:Key="V2C"></local:ValueToColorConverter>
</Window.Resources>
<Grid>
<DataGrid ItemsSource="{Binding Items}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Grid Background="{Binding IsRead,Converter={StaticResource V2C}}"></Grid>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
MainWindow.xaml.cs:
using System.Windows;
namespace WpfApplication12
{
public partial class MainWindow : Window
{
ViewModel vm;
public MainWindow()
{
this.Loaded += MainWindow_Loaded;
InitializeComponent();
}
void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
vm = new ViewModel();
this.DataContext = vm;
}
}
}
ViewModel.cs:
using System.Collections.ObjectModel;
namespace WpfApplication12
{
class ViewModel : ViewModelBase
{
public ViewModel()
{
items = new ObservableCollection<item>();
Items.Add(new Item() { IsRead = true });
Items.Add(new Item() { IsRead = false });
Items.Add(new Item() { IsRead = true });
Items.Add(new Item() { IsRead = false });
Items.Add(new Item() { IsRead = true });
}
private ObservableCollection<item> items;
public ObservableCollection<item> Items
{
get
{
return items;
}
set
{
if (items != value)
{
items = value;
OnPropertyChanged();
}
}
}
}
}
ViewModelBase.cs:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WpfApplication12
{
class ViewModelBase : INotifyPropertyChanged
{
[AttributeUsageAttribute(AttributeTargets.Parameter, Inherited = false)]
public sealed class CallerMemberNameAttribute : Attribute { }
public event PropertyChangedEventHandler PropertyChanged = delegate { };
public void OnPropertyChanged([CallerMemberName]string propertyName = null)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}
Item.cs:
namespace WpfApplication12
{
class Item
{
public bool IsRead { get; set; }
}
}