Search code examples
c#wpfdatagridmultivalue

WPF datagrid get multiple cell values from a row without selection


I'm currently working on a WPF application in C#. My DataGrid contains a lot of columns:

    <DataGrid x:Name="ProductLijstDatagrid"  MaxWidth="1700" Style="{StaticResource CustomDataGridStyle}" Grid.Row="13" Grid.ColumnSpan="5" Margin="15,0,15,0"  MaxHeight="350">
                        <DataGrid.Columns>
                            <DataGridTextColumn Header="Vlarema" Binding="{Binding Vlarema}">
<DataGridTextColumn.ElementStyle>
                                <Style TargetType="{x:Type TextBlock}">
                                    <Setter Property="Background" Value="{Binding Vlarema, Converter={StaticResource VlaremaBrushConverter}}"/>
                                </Style>
                            </DataGridTextColumn.ElementStyle>
                    </DataGridTextColumn>
                            <DataGridTextColumn Header="PFas" Binding="{Binding PFas}"/>
                            <DataGridTextColumn Header="RiskCat" Binding="{Binding RiskCat}"/>
    </DataGrid.Columns>
                </DataGrid>

What i need to do is make sure if the RiskCat is 2 or 3 and the Vlarema cell (which is a date) is older then 1 year the background should be colored red. This is something like a warning. Also have already the solution for if no date was given which is also a kind of warning so the cell will get the red color too. I'm looking for a Solution to get those two values in one class. I've tried with the IValueConverter, which works but this is only for one value i guess. This is the result i have at the moment:

public class VlaremaBrushConverter : IValueConverter
    {
        RisicocategorieConverter rc = new RisicocategorieConverter();
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            string riskCat = "";
            string input = (string)value;
            switch (input)
            {
                case "00/00/0000": return Brushes.Red;              
                default: return DependencyProperty.UnsetValue;
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotSupportedException();
        }
    }

As you can see i have a variable which is called RiskCat, This is were i need the RiskCat to do the further calculations...

Also have been trying with IMultiValueConverter but im not sure if this works :

public class RiskCatConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            if (values.Length > 0 && values[0] is int risicocategorie)
            {
                if (risicocategorie == 2 || risicocategorie == 3)
                {
                    return risicocategorie;
                }
            }

            return false;
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            throw new NotSupportedException();
        }

        public int GetRisicoCategorie(DataGridCell cell)
        {
            
            if (cell.Content is TextBlock textBlock && textBlock.GetBindingExpression(TextBlock.TextProperty) is BindingExpression bindingExpression)
            {
                
                var dataRow = bindingExpression.DataItem as DataRowView;

                
                if (dataRow != null && dataRow.Row.Table.Columns.Contains("Risicocategorie"))
                {
                    
                    int risicocategorie = (int)dataRow["Risicocategorie"];
                    return risicocategorie;
                }
            }

            
            return 0;
        }

Solution

  • Most simplistic approach I can think of: pass the entire object to your converter. In your style change the binding - note that 'Vlarema' is now ommitted. With no path specified, the binding will resolve to the DataContext as a whole.

    <Setter Property="Background" Value="{Binding, Converter={StaticResource VlaremaBrushConverter}}"/>
    

    Then, in your converter, just cast the value to your row object.

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        var row = (YourModelObject) value;  //assuming 'YourModelObject' being your rows type of DataContext
    
        if(row.RiskCat == 2 || row.RiskCat == 3){
            if(string.Equals(row.Vlarema, "00/00/0000"){
                return Brushes.Red; 
            }
        }
                
        return DependencyProperty.UnsetValue;
    }