简体   繁体   中英

WPF DataGrid row color change based on variable

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'

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);

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>

refer this link

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM