Reputation: 78
I'm using data triggers to change the color of the rows in a DataGrid view component. the code is:
class QuantityToColorConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return (int)value <= 444 ?
new SolidColorBrush(Colors.Red)
: new SolidColorBrush(Colors.White);
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new Exception("The method or operation is not implemented.");
}
}
How can I replace the '444' value by a variable that has the result of some computation of the grid cell values in the button_click function?
EDIT: to make it more clear what I need: I want to change the color of the rows which have the value in one of the columns bigger than the average . As the average is computed based on the DataGrid data, I need to send it as a variable instead of the 444 constant.
EDIT2: the button code:
private void Button_Click_1(object sender, RoutedEventArgs e)
{
var engine = new FileHelperEngine<ActionLog>();
var result = engine.ReadFile("browse.csv");
// result is now an array of ActionLog
var actionsCnt = new int[22];
int curAccessId = result[1].AccessId;
int AccessCount = 0;
foreach (var record in result)
{
actionsCnt[record.ActionType]++;
if (record.AccessId != curAccessId) { curAccessId = record.AccessId; AccessCount++; }
}
quantityThreshold = AccessCount;
List<act> myList = new List<act>();
for (int i = 0; i < 22; i++)
myList.Add(new act() { actionID = i, quantity = actionsCnt[i] });
grid1.ItemsSource = myList;
engine.WriteFile("FileOut.csv", result);
}
quantityThreshold is the variable I want to use instead of '444'
Upvotes: 2
Views: 1311
Reputation: 47570
Bind that computed variable to your Converter's ConverterParameter
. See this thread on Binding to Converter Parameter
return (int)value <= (int)parameter?
new SolidColorBrush(Colors.Red)
: new SolidColorBrush(Colors.White);
Upvotes: 2
Reputation: 437
You could pass parameter to converter, like this
...
Binding="{Binding ValueToBind, Converter={SomeConverter},ConverterParameter=YourParameteres}"
...
And then use it in your converter as object parameter
Upvotes: 0
Reputation: 2206
try this
<DataGrid>
<DataGrid.CellStyle>
<Style TargetType="{x:Type DataGridCell}">
<Style.Triggers>
<DataTrigger Binding="{Binding SomeProperty}" Value="SomeValue" >
<Setter Property="Foreground" Value="Blue" />
</DataTrigger>
</Style.Triggers>
</Style>
</DataGrid.CellStyle>
</DataGrid>
Upvotes: 1