Answer 7
As I now use this a lot, I thought I'd share what we did as I know how frustrating it is trying to locate this information.
Where, in this example, my project (namespace) is called DataGridColrs.
In the header part of the XAML I added:
xmlns:local="clr-namespace:DataGridColrs"
Then in the window.resources area I add the lines:
<local:BGConverter x:Key="myBGColor"/>
<local:FGConverter x:Key="myFGColor"/>
and the
code to set a default
cell style thus:
<Style x:Key="defCStyle" TargetType="{x:Type dg:DataGridCell}">
<Setter Property="Foreground">
<Setter.Value>
<MultiBinding Converter="{StaticResource myFGColor}" >
<MultiBinding.Bindings>
<Binding RelativeSource="{RelativeSource Self}">
</Binding>
<Binding Path="Row"></Binding>
</MultiBinding.Bindings>
</MultiBinding>
</Setter.Value>
</Setter>
<Setter Property="Background">
<Setter.Value>
<MultiBinding Converter="{StaticResource myBGColor}" >
<MultiBinding.Bindings>
<Binding RelativeSource="{RelativeSource Self}">
</Binding>
<Binding Path="Row"></Binding>
</MultiBinding.Bindings>
</MultiBinding>
</Setter.Value>
</Setter>
</Style>
In my code for the datagrid, I add the line to set the cellstyle to the above
<dg:DataGrid Margin="12,12,23,60"
CellStyle="{StaticResource defCStyle}"
Name="dgrid1" IsReadOnly="True">
</dg:DataGrid>
..Then in the code I create the foreground and
background converters
within the namespace but outside of the window class thus:
(in this case, I am reacting to the physical stock level).
public class BGConverter : IMultiValueConverter
{
#region Implementation of BGConverter
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
SolidColorBrush mybrush = new SolidColorBrush();
Decimal cell_value;
if (values[1] is DataRow)
{
var cell = (DataGridCell)values[0];
var
row = (DataRow)values[1];
var columnName = cell.Column.SortMemberPath;
Type type1 = row[columnName].GetType();
if (columnName == "physical_stock")
{
cell_value = (Decimal)row[columnName]; // phys. stock is a Decimal column
if (cell_value <= 0)
mybrush = new SolidColorBrush(Colors.Red);
else if (cell_value <= 8)
mybrush = new SolidColorBrush(Colors.Yellow);
else if (cell_value < 20)
mybrush = new SolidColorBrush(Colors.Green);
else
mybrush = new SolidColorBrush(Colors.White);
}
else
{
mybrush = new SolidColorBrush(Colors.AliceBlue);
}
return mybrush;
}
return SystemColors.AppWorkspaceColor;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new System.NotImplementedException();
}
#endregion
}
// ----------------------------------------
public class FGConverter : IMultiValueConverter
{
#region Implementation of FGConverter
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
SolidColorBrush mybrush = new SolidColorBrush();
Decimal cell_value;
if (values[1] is DataRow)
{
var cell = (DataGridCell)values[0];
var row = (DataRow)values[1];
var columnName = cell.Column.SortMemberPath;
Type type1 = row[columnName].GetType();
if (columnName == "physical_stock")
{
cell_value = (Decimal)row[columnName];
if (cell_value <= 0)
mybrush = new SolidColorBrush(Colors.White);
else if (cell_value <= 8)
mybrush = new SolidColorBrush(Colors.Black);
else if (cell_value < 20)
mybrush = new SolidColorBrush(Colors.White);
else
mybrush = new SolidColorBrush(Colors.Black);
}
else
{
mybrush = new SolidColorBrush(Colors.Navy);
}
return mybrush;
}
return Colors.Black;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new System.NotImplementedException();
}
#endregion
}
..Hope this helps.
-Terian