Change color of DataGrid Cell WPF regarding Range
Hi I need to implement a function that if the value of the binding items is within the specific range cell color should be according to the range.
I have been using Changing Background Color Of DataGrid Cell WPF 4
this works fine but it is for only if that values are there.what if i want to add range i.e from 10 - 20 it is red 21-30 it is blue
added everything and saw an example at the end but the color does not change here is the code
Class
public class ConvertToBrush : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
int tempValue = int.Parse(value.ToString());
string tempString = "Red";
if (tempValue >= 0 && tempValue <= 20)
tempString = "#FF0000";
if (tempValue > 20 && tempValue <= 40)
tempString = "#F09300";
if (tempValue > 40 && tempValue <= 60)
tempString = "#EDDF00";
if (tempValue > 60 && tempValue <= 80)
tempString = "#FFFFFF";
if (tempValue > 80 && tempValue <= 100)
tempString 开发者_如何学C= "#85AB00";
SolidColorBrush brush = new SolidColorBrush();
BrushConverter conv = new BrushConverter();
brush = conv.ConvertFromString(tempString) as SolidColorBrush;
return brush;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return DependencyProperty.UnsetValue;
}
}
XMAL
<DataGridTextColumn ElementStyle="{StaticResource CentreAlignStyle}" Binding="{Binding TestResults}" Header="Results" IsReadOnly="True" MaxWidth="60" MinWidth="60" >
<DataGridTextColumn.CellStyle>
<Style>
<Setter Property="TextBlock.Background" Value="{Binding TestResults, Converter={StaticResource makeBrush}}" />
</Style>
</DataGridTextColumn.CellStyle>
</DataGridTextColumn>
Don't use a DataTrigger
but just bind the Background
to the value and put in a ValueConverter
to return the right brush (or no brush at all).
Edit: What the usage should look like:
<DataGridTextColumn.CellStyle>
<Style>
<Setter Property="Border.Background" Value="{Binding TestResults, Converter={StaticResource BrushConverter}}" />
</Style>
</DataGridTextColumn.CellStyle>
I'm not sure i understood you right, but here goes:
So let's say that "TestResults" contains the value you're talking about. In XAML:
<DataGridTextColumn TextBlock.Background={Binding TestResults, Converter={StaticResource makeBrush}} />
Just to make sure we're on the same page here, you define the converter in your XAML like this:
<Window.Resources>
<local:makeBrush x:Key="makeBrush" />
</Window.Resources>
In the makeBrush converter, you do this:
public class makeBrush : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
int theValueToConvertToColor = (int)value;
if (theValueToConvertToColor > 10 && theValueToConvertToColor <= 20)
{
return Brushes.Red;
}
if (theValueToConvertToColor > 20 && theValueToConvertToColor <= 30)
{
return Brushes.Blue;
}
//More ifs...
else return Brushes.Green;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return value;
}
}
The "object value
" you get in the Convert
method of your IValueConverter
is actually the value of "TestResults
"
Note: I didn't actually test it with a DataGridTextColumn
, but i guess you get the point and can make the necessary adjustments if required.
精彩评论