0

我正在使用 WPF DataGrid 并通过使用类 RowItem 在运行时添加行

 public class RowItem //Class
 {
     public int Rule_ID { get; set; }
     public string Rule_Desc { get; set; }
     public Int64 Count_Of_Failure { get; set; }
 }    

adding row at run time like :

dgValidateRules.Items.Add(new RowItem() { Rule_ID = ruleID, Rule_Desc = ruleDesc, Count_Of_Failure = ttlHodlings });

使用下面的 Loading Row 事件代码来更改数据网格行的颜色。但它不起作用。

private void dgValidateRules_LoadingRow(object sender, DataGridRowEventArgs e)
{
  for (int i = 1; i < dgValidateRules.Items.Count; i++)
  {
    if (((RowItem)dgValidateRules.Items[i]).Count_Of_Failure == 0)
      e.Row.Foreground = new SolidColorBrush(Colors.Black);
    else
      e.Row.Foreground = new SolidColorBrush(Colors.Red);
  }
}

谁能告诉我解决方案?

4

2 回答 2

0

因为它是行事件,所以它是做这件事的好地方,在这里你可以放置一个关于行的条件:

    private void table_LoadingRow(object sender, DataGridRowEventArgs e)
    {
        if (((MyData)e.Row.DataContext).Module.Trim().Equals("SomeText"))
        {
            e.Row.Foreground = new SolidColorBrush(Colors.Red);
        }
    }
于 2017-10-29T09:56:09.880 回答
0

您可以使用DataTriggerConverter

<DataGrid ItemsSource="{Binding YourItemsSource}">
    <DataGrid.RowStyle>
        <Style TargetType="DataGridRow"> 
            <Style.Triggers>
                <DataTrigger Binding="{Binding Count_Of_Failure}" Value="0">
                    <Setter Property="Foreground" Value="Red"></Setter>
                </DataTrigger>
                <DataTrigger Binding="{Binding Count_Of_Failure}" Value="1">
                    <Setter Property="Foreground" Value="Green"></Setter>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </DataGrid.RowStyle>
</DataGrid>
于 2017-10-29T11:20:03.813 回答