2

我正在制作一个 WPF 程序,它能够DataGrid使用循环将行逐一着色为红色for,但我遇到了一些奇怪的事情。如果DataGrid数据库表中的数据超过 40 行,它不会为所有行着色。

这是我正在使用的代码。

private void Red_Click(object sender, RoutedEventArgs e)
{
    for (int i = 0; i < dataGrid1.Items.Count; i++)
    {
        DataGridRow row = (DataGridRow)dataGrid1.ItemContainerGenerator.ContainerFromIndex(i);
        if (row != null)
        {
            row.Background = Brushes.Red;
        }
    }
}

有没有其他方法可以通过其他方法对行逐个着色,或者这是 wpftoolkit 中的某种错误?

4

3 回答 3

4

如果要为每一行定义颜色,并且在行显示的项目上有一个属性,则可以使用 ItemsContainerStyle 来设置行颜色。在下面的示例中,您将在网格中的项目上拥有一个名为 ItemColour 的属性,该属性将定义背景行颜色。绑定从行绑定到该行包含的项目。

 <dg:DataGrid.ItemContainerStyle>
    <Style
       TargetType="{x:Type dg:DataGridRow}"
       BasedOn="{StaticResource {x:Type dg:DataGridRow}}">
       <Setter
          Property="Background"
          Value="{Binding ItemColour}" />
    </Style>
 </dg:DataGrid.ItemContainerStyle>

但是您可能不希望在您的项目上使用属性 ItemColour,因为它们可能是您的业务模型。这就是 ViewModel 发挥作用的地方。您定义了一个中间层,该层包含您的业务层和基于一些自定义逻辑的 ItemColour 属性。

于 2010-01-12T05:48:03.210 回答
2

如果要为网格的所有行设置背景,可以定义一个新的行样式对象并设置其背景属性;这应该一次更改所有行背景,而无需遍历它们。像这样:

dataGrid1.RowStyle = new Style(typeof(DataGridRow));
dataGrid1.RowStyle.Setters.Add(new Setter(Control.BackgroundProperty, new SolidColorBrush(Colors.Red))); 

您还可能需要根据数据网格行背后的数据对象的状态来更改它们的背景。在这种情况下,您可以在 xaml 中使用触发器设置自定义样式并将其分配给 rowstyle。我猜是这样的:

<Window.Resources>
    <Style x:Key="customDataGridRowStyle" TargetType="{x:Type DataGridRow}">
        <Style.Triggers>
            <DataTrigger Binding="{Binding Test1}" Value="1">
                <Setter Property="Background" Value="Red"/>
            </DataTrigger>
        </Style.Triggers>
    </Style>
</Window.Resources>
..
<DataGrid .. DataGrid.RowStyle ="{StaticResource customDataGridRowStyle}" >
..

在上面的示例中,只要“Test1”属性的值为“1”,就会将红色背景设置为该行

希望这会有所帮助,问候

于 2010-01-12T04:52:02.230 回答
0

屏幕上不可见的行将无法使用此方法着色,因为它们已被虚拟化并且实际上并不存在。在下面的样式中,我绑定到属性 IsRed 以将行转换为红色和它们的默认颜色(将其放在带有数据网格的 from 的资源中)

        <Style
           TargetType="{x:Type dg:DataGridRow}"
           BasedOn="{StaticResource {x:Type dg:DataGridRow}}">
           <Style.Triggers>
              <DataTrigger
                 Binding="{Binding ElementName=self, Path=IsRed}"
                 Value="True">
                 <Setter
                    Property="Background"
                    Value="Red" />
              </DataTrigger>
           </Style.Triggers>
        </Style>

我的表单上有一个名为 IsRed 的依赖属性,这也可以是实现 INotifyPropertyChanged 的​​任何属性(依赖属性通知它们的更改)

  public Boolean IsRed {
     get { return (Boolean)GetValue(IsRedProperty); }
     set { SetValue(IsRedProperty, value); }
  }

  // Using a DependencyProperty as the backing store for IsRed.  This enables animation, styling, binding, etc...
  public static readonly DependencyProperty IsRedProperty =
      DependencyProperty.Register("IsRed", typeof(Boolean), typeof(Window1), new UIPropertyMetadata(false));

然后在我的xaml中,我在顶部有声明

<Window
   x:Class="Grids.Window1"
   x:Name="self">

这意味着我可以使用元素名称绑定来引用它(我觉得很有用的一种技术)

使用我概述的代码,您只需单击所有按钮即可

  private void Button_Click(object sender, RoutedEventArgs e) {
     IsRed = !IsRed;
  }
于 2010-01-12T03:00:20.247 回答