【问题标题】:How to change the background color of a certain row in a DataGrid如何更改 DataGrid 中某一行的背景颜色
【发布时间】:2015-03-26 22:05:46
【问题描述】:

例如,在 xaml 中,我有一个名为 PersonList 的 DataGrid:

<DataGrid Name="PersonList" />

在代码隐藏中我有一个 Person 集合:

ObservableCollection<Person> persons = ViewModel.PersonModel;

然后我创建了一个Person DataTable,并通过以下方式将它绑定到PersonList:

PersonDataTable.Columns.Add("Name", typeof(string));
PersonDataTable.Columns.Add("Age", typeof(int));

foreach (var person in persons)
{
    if (person != null)
    {
        PersonDataTable.Rows.Add(
            Person.Name,
            Person.Age
            );
    }
}
PersonList.ItemSource = PersonDataTable.AsDataView;

我的问题是,如何改变某一行的背景颜色?例如,更改人的年龄> 50的行的背景颜色

我尝试通过访问 PersonList.ItemSource 中的每一行来做到这一点,但我失败了,该行始终为空:

int count = 0;
foreach (var person in PersonList.ItemSource)
{
    var row = PersonList.ItemContainerGenerator.ContainerFromItem(person) as DataGridRow;
    if (PersonDataTable.Rows[count].Field<int>(1) > 50)
    {
        row.Background = Brushes.Gray;
    }
    count++;
}

请帮助,WPF 大师 :)

【问题讨论】:

    标签: c# wpf datagrid datatable


    【解决方案1】:

    使用转换器尝试您的逻辑,如下所示:

    这是我的 AgeAboveLimitConverter 文件:

    using System;
    using System.Windows.Data;
    
    namespace DataGridSample.Converter
    {
        public class AgeAboveLimitConverter : IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                if (value != null)
                {
                    return (int)value > 50;
                }
    
                return false;
            }
    
            public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                return null;
            }
        }
    }
    

    然后在您的 datagrid xaml 文件中, 添加命名空间xmlns:converter="clr-namespace:DataGridSample.Converter"

    为DataGrid中的DataGridRow添加样式,

    <Grid>
            <Grid.Resources>
                <converter:AgeAboveLimitConverter x:Key="AgeConverter"/>
            </Grid.Resources>
            <DataGrid Name="PersonList">
                <DataGrid.RowStyle>
                    <Style TargetType="DataGridRow" >
                        <Setter Property="Background"  Value="Transparent" />
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding Path=Age,Converter={StaticResource AgeConverter}}" Value="true">
                                <Setter Property="Background" Value="Red"/>
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </DataGrid.RowStyle>
            </DataGrid>
        </Grid>
    

    【讨论】:

      【解决方案2】:

      你快到了。请尝试以下操作:

      int count = 0;
      foreach (var person in PersonList.ItemSource)
      {
          var row = PersonList.ItemContainerGenerator.ContainerFromItem(person) as DataGridRow;
          if (PersonDataTable.Rows[count].Field<int>(1) > 50)
          {
              row.DefaultCellStyle.BackColor = Color.Gray; 
          }
          count++;
      }
      

      【讨论】:

      猜你喜欢
      • 2011-05-11
      • 2015-08-22
      • 1970-01-01
      • 2016-11-15
      • 1970-01-01
      • 2015-08-31
      • 2023-01-16
      • 1970-01-01
      • 2010-09-24
      相关资源
      最近更新 更多