【问题标题】:Coloring WPF DataGridRows one by one一一着色 WPF DataGridRows
【发布时间】:2010-01-12 01:31:50
【问题描述】:

我正在制作一个 WPF 程序,它能够使用 for 循环将 DataGrid 中的行一一着色为红色,我遇到了一些奇怪的事情。如果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 中的某种错误?

【问题讨论】:

    标签: wpf database datagrid wpfdatagrid wpftoolkit


    【解决方案1】:

    如果您想为每一行定义颜色,并且您在行显示的项目上有一个属性,您可以使用 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 属性。

    【讨论】:

    • 你能提供一个关于这个 ViewModel 的例子吗?在绑定属性方面我还是不好。
    • sites.google.com/site/wpfprojects 下载彩色行示例,并将此问题标记为已回答。那里可能还有其他一些您可能想查看的项目
    • +1 表示用 'u' 拼写颜色,哦,谢谢你的例子。
    【解决方案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”,就会将红色背景设置为该行

    希望这会有所帮助,问候

    【讨论】:

      【解决方案3】:

      屏幕上不可见的行将无法使用此方法着色,因为它们已被虚拟化并且实际上并不存在。在下面的样式中,我绑定到属性 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;
        }
      

      【讨论】:

      • 我明白了。如果行不存在,因为它们在数据网格上看不到,这是否意味着不能用一种以上的颜色(例如:白色/黑色/灰色或黄色/红色/蓝色)为行着色?谢谢你的回答顺便说一句。我仍然觉得必须有其他方法来解决这个问题。
      猜你喜欢
      • 2014-08-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-03
      • 2011-06-15
      • 1970-01-01
      相关资源
      最近更新 更多