[英]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 我一直在使用更改DataGrid单元格WPF 4的背景色
this works fine but it is for only if that values are there.what if i want to add range ie from 10 - 20 it is red 21-30 it is blue 这工作正常,但仅适用于该值存在的地方。如果我要添加范围,即从10-20,则为红色21-30,它为蓝色
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 = "#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 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). 不要使用DataTrigger
而只需将Background
绑定到该值,然后将其ValueConverter
以返回正确的画笔(或根本没有画笔)。
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. 因此,假设“ TestResults”包含您正在谈论的值。 In XAML: 在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: 为了确保我们在同一页面上,您可以在XAML中定义转换器,如下所示:
<Window.Resources>
<local:makeBrush x:Key="makeBrush" />
</Window.Resources>
In the makeBrush converter, you do this: 在makeBrush转换器中,您可以执行以下操作:
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
" 您在IValueConverter
的Convert
方法中获得的“ object value
”实际上是“ 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. 注意:我实际上并没有使用DataGridTextColumn
对其进行测试,但是我想您已经明白了这一点,并且可以根据需要进行必要的调整。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.