Files to look at:
- BindingToColorConverter.cs (VB: BindingToColorConverter.vb)
- CellsHightlightHelper.cs (VB: CellsHightlightHelper.vb)
- HighlightedGridCell.cs (VB: HighlightedGridCell.vb)
- MainWindow.xaml (VB: MainWindow.xaml)
- MainWindow.xaml.cs (VB: MainWindow.xaml.vb)
- DataHelper.cs (VB: DataHelper.vb)
- ViewModel.cs (VB: ViewModel.vb)
To change a specific grid cell color, use the solution from the Styles and Templates Overview article.
In case of a simple scenario, when you need to highlight a cell based on its value or some other property that is available in the current row object, just specify a correct binding. For example:
XAML<Style x:Key="customCellStyle" BasedOn="{StaticResource {dxgt:GridRowThemeKey ResourceKey=CellStyle}}" TargetType="dxg:CellContentPresenter">
<Setter Property="Background" Value="{Binding Path=RowData.Row.SomeFieldName, Converter={local:YourConverter}}"/>
</Style>
Optimized mode
<Style x:Key="customCellStyle" BasedOn="{StaticResource {dxgt:GridRowThemeKey ResourceKey=LightweightCellStyle}}" TargetType="dxg:LightweightCellEditor">
<Setter Property="Background" Value="{Binding Path=RowData.Row.SomeFieldName, Converter={local:YourConverter}}"/>
</Style>
However, if your cells should be colored based on complex logic, then a simple binding won't help you. In this case, it is better to create an attached property and bind to this property. Then, update this property when it is necessary.
For example, if a specific row color depends on other rows, handle data changes in your datasource and update your attached property based on these changes.
This example demonstrates the main idea of how to implement this functionality.
See also:
How to change background color for modified cells
Does this example address your development requirements/objectives?
(you will be redirected to DevExpress.com to submit your response)
Example Code
C#// Developer Express Code Central Example:
// A general approach to highlighting specific grid cells
//
// To change a specific grid cell color, use the solution from the Styles and
// Templates Overview (http://documentation.devexpress.com/#WPF/CustomDocument6762)
// article.
//
// In case of a simple scenario, when you need to highlight a cell
// based on its value or some other property that is available in the current row
// object, just specify a correct binding. For example:
// </para><para><Style
// x:Key="customCellStyle"</para><para> BasedOn="{StaticResource
// {dxgt:GridRowThemeKey ResourceKey=CellStyle}}"</para><para>
// TargetType="dxg:CellContentPresenter"></para><para> <Setter
// Property="Background"</para><para> Value="{Binding
// Path=RowData.Row.SomeFieldName, Converter={local:YourConverter}}"/></para><para>
// </Style></para><para>
// However, if your cells should be colored based on
// complex logic, then a simple binding won't help you. In this case, it is better
// to create an attached property and bind to this property. Then, update this
// property when it is necessary. For example, if a specific row color depends on
// other rows, handle data changes in your datasource and update your attached
// property based on these changes. This example demonstrates the main idea of how
// to implement this functionality.
// See
// also:
//
// http://www.devexpress.com/scid=E1297
//
// You can find sample updates and versions for different programming languages here:
// http://www.devexpress.com/example=E4181
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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 System.Windows.Markup;
using System.Collections.ObjectModel;
using DevExpress.Xpf.Grid;
namespace WpfApplication {
public class BindingToColorConverter : DependencyObject, IMultiValueConverter {
object IMultiValueConverter.Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
ObservableCollection<HighlightedGridCell> cellsToHighlight = values[0] as ObservableCollection<HighlightedGridCell>;
object row = values[1];
object column = values[2];
return GetColorToHighlight(row, column, cellsToHighlight);
}
object[] IMultiValueConverter.ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture) {
throw new NotImplementedException();
}
private object GetColorToHighlight(object row, object column, ObservableCollection<HighlightedGridCell> cellsToHighlight) {
if (row == null || column == null || cellsToHighlight == null)
return null;
foreach (HighlightedGridCell cell in cellsToHighlight) {
if (cell.Column == column && cell.Row == row)
return new SolidColorBrush(cell.Color);
}
return null;
}
}
}
C#using System.Collections.ObjectModel;
using System.Windows;
using DevExpress.Xpf.Grid;
namespace WpfApplication {
public static class CellsHightlightHelper {
public static readonly DependencyProperty CellsToHighlightProperty = DependencyProperty.RegisterAttached("CellsToHighlight", typeof(ObservableCollection<HighlightedGridCell>), typeof(CellsHightlightHelper), null);
public static ObservableCollection<HighlightedGridCell> GetCellsToHighlight(DependencyObject target) {
return (ObservableCollection<HighlightedGridCell>)target.GetValue(CellsToHighlightProperty);
}
public static void SetCellsToHighlight(GridControl target, ObservableCollection<HighlightedGridCell> value) {
target.SetValue(CellsToHighlightProperty, value);
}
}
}
C#using System.Windows.Media;
using DevExpress.Xpf.Grid;
namespace WpfApplication {
public class HighlightedGridCell {
/// <summary>
/// Initializes a new instance of the HighlightedGridCell class.
/// </summary>
/// <param name="row"></param>
/// <param name="column"></param>
/// <param name="color"></param>
public HighlightedGridCell(object row, GridColumn column, Color color) {
_Color = color;
_Row = row;
_Column = column;
}
// Fields...
private Color _Color;
private object _Row;
private GridColumn _Column;
public GridColumn Column {
get { return _Column; }
set {
_Column = value;
}
}
public object Row {
get { return _Row; }
set {
_Row = value;
}
}
public Color Color {
get { return _Color; }
set {
_Color = value;
}
}
}
}
XAML<Window x:Class="WpfApplication.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication"
xmlns:dxgt="http://schemas.devexpress.com/winfx/2008/xaml/grid/themekeys"
xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid"
Height="350"
Width="525"
Title="MainWindow">
<Window.DataContext>
<local:ViewModel />
</Window.DataContext>
<DockPanel>
<Button Click="button1_Click"
Content="Button"
DockPanel.Dock="Bottom"
Name="button1" />
<dxg:GridControl ItemsSource="{Binding DataSource}" Name="gridControl1">
<dxg:GridControl.Columns>
<dxg:GridColumn FieldName="Name" />
<dxg:GridColumn FieldName="ID" />
<dxg:GridColumn FieldName="Date" />
</dxg:GridControl.Columns>
<dxg:GridControl.View>
<dxg:TableView Name="tableView1">
<dxg:TableView.CellStyle>
<Style TargetType="dxg:LightweightCellEditor">
<Style.Resources>
<local:BindingToColorConverter x:Key="converter" />
</Style.Resources>
<Setter Property="Background">
<Setter.Value>
<MultiBinding Converter="{StaticResource converter}">
<Binding Path="(local:CellsHightlightHelper.CellsToHighlight)" RelativeSource="{RelativeSource AncestorType=dxg:GridControl}" />
<Binding Path="RowData.Row" />
<Binding Path="Column" />
</MultiBinding>
</Setter.Value>
</Setter>
</Style>
</dxg:TableView.CellStyle>
</dxg:TableView>
</dxg:GridControl.View>
</dxg:GridControl>
</DockPanel>
</Window>
C#using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Media;
namespace WpfApplication {
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window {
public MainWindow() {
InitializeComponent();
ObservableCollection<HighlightedGridCell> cellsToHiglight = new ObservableCollection<HighlightedGridCell>();
cellsToHiglight.Add(new HighlightedGridCell(gridControl1.GetRow(0), gridControl1.Columns["ID"], Colors.Red));
CellsHightlightHelper.SetCellsToHighlight(gridControl1, cellsToHiglight);
}
private void button1_Click(object sender, RoutedEventArgs e) {
ObservableCollection<HighlightedGridCell> cellsToHiglight = new ObservableCollection<HighlightedGridCell>();
cellsToHiglight.Add(new HighlightedGridCell(gridControl1.GetRow(1), gridControl1.Columns["Name"], Colors.Yellow));
cellsToHiglight.Add(new HighlightedGridCell(gridControl1.GetRow(1), gridControl1.Columns["Date"], Colors.Orange));
CellsHightlightHelper.SetCellsToHighlight(gridControl1, cellsToHiglight);
}
}
}
C#using System;
using System.Collections.ObjectModel;
namespace WpfApplication {
public class DataHelper {
public static object GetDataSource(int count) {
ObservableCollection<GridItem> collection = new ObservableCollection<GridItem>();
for (int i = 0; i < count; i++) {
collection.Add(new GridItem(DateTime.Now.AddMinutes(count * i).AddDays((i - count / 2) * count), String.Format("Name{0}", i), 0));
}
return collection;
}
}
public class GridItem {
/// <summary>
/// Initializes a new instance of the GridItem class.
/// </summary>
/// <param name="date"></param>
/// <param name="name"></param>
/// <param name="iD"></param>
public GridItem(DateTime date, string name, int iD) {
_Date = date;
_Name = name;
_ID = iD;
}
public GridItem() {
}
// Fields...
private DateTime _Date;
private string _Name;
private int _ID;
public string Name {
get { return _Name; }
set {
_Name = value;
}
}
public int ID {
get { return _ID; }
set {
_ID = value;
}
}
public DateTime Date {
get { return _Date; }
set {
_Date = value;
}
}
}
}
C#using System;
using System.Collections.Generic;
using System.Linq;
namespace WpfApplication {
public class ViewModel {
// Fields...
private object _DataSource;
public object DataSource {
get {
if (_DataSource == null)
_DataSource = DataHelper.GetDataSource(1020);
return _DataSource;
}
set {
_DataSource = value;
}
}
}
}