WPF数据网格从一行中获取多个单元格值而不进行选择

2izufjch  于 2023-05-19  发布在  其他
关注(0)|答案(1)|浏览(99)

我目前正在用C#开发一个WPF应用程序。我的DataGrid包含很多列:

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

我需要做的是确保如果RiskCat是2或3和Vlarema细胞(这是一个日期)是旧的,然后1年的背景应该是红色的。这是一个警告。也已经有了解决方案,如果没有日期,这也是一种警告,使细胞将获得红色太。我正在寻找一个解决方案,让这两个值在一个类。我已经尝试了IValueConverter,它可以工作,但我猜这只是一个值。这是我目前得到的结果:

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

正如你所看到的,我有一个名为RiskCat的变量,这是我需要RiskCat做进一步计算的地方...
也一直在尝试与IMultiValueConverter,但我不知道这是否工作:

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;
        }
tf7tbtn2

tf7tbtn21#

我能想到的最简单的方法:将整个对象传递给转换器。在你的风格改变装订-注意'Vlarema'现在省略。如果不指定路径,绑定将解析为整个DataContext。

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

然后,在转换器中,只需将值转换为行对象。

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

相关问题