简体   繁体   English

循环遍历Silverlight DataGrid中的行

[英]Loop through rows in Silverlight DataGrid

I have a feeling i'm missing something obvious here, but i can not find a way to iterate through a DataGrids DataGridRow collection. 我有一种感觉,我在这里遗漏了一些明显的东西,但我无法找到迭代DataGrids DataGridRow集合的方法。 I have a grid which has an itemssource of a collection of my class set. 我有一个网格,其中包含我的类集合的itemssource。 I am trying to iterate through the rows and highlight any rows that meet a certain condition but can't for the life of me see how. 我试图迭代行并突出显示符合某种条件的任何行,但不能为我的生活看到如何。

You don't want to iterate through the grid. 您不想遍历网格。 That's old-skool WinForms thinking. 这是一个老式的WinForms思考。 The Grids in WPF and Silverlight have been redesigned with MVVM in mind; WPF和Silverlight中的网格已经重新设计了MVVM; with separation of concerns. 关注分离。 Instead of manipulating the grid, you work directly with your objects that are bound to the grid. 您可以直接使用绑定到网格的对象,而不是操纵网格。 So the grid just becomes a presentation concern. 所以网格只是一个演示问题。 Its responsibility is to read the objects and display information based on the data in those objects. 它的职责是根据这些对象中的数据读取对象并显示信息。

What you want to do instead is attach properties to the object that you're binding to and have the grid set styles for color/font/etc., based on those settings. 您要做的是将属性附加到您要绑定的对象,并根据这些设置具有颜色/字体/等的网格集样式。 To do this, you'll need to create an IValueConverter. 为此,您需要创建一个IValueConverter。

Here is an example of a converter I have in a WPF and Silverlight datagrid: 以下是我在WPF和Silverlight数据网格中的转换器示例:

public class StateToBackgroundColorConverter : IValueConverter
  {
    #region IValueConverter Members

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
      if (value == null) return Colors.White.ToString();

      var state = (State) value;
      return state.WebColor;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
      throw new NotImplementedException();
    }

    #endregion
  }

In my XAML I declare it like this: 在我的XAML中,我声明它是这样的:

<UserControl.Resources>
    <Converters:StateToBackgroundColorConverter x:Key="stateToBackgroundColorConverter"/>
</UserControl.Resources>

In the datagrid declaration in XAML I specify the converter usage for the DataGridRow: 在XAML的datagrid声明中,我指定了DataGridRow的转换器用法:

 <toolkit:DataGrid.RowStyle>
          <Style TargetType="{x:Type toolkit:DataGridRow}">
            <Style.Setters>
              <Setter Property="FontWeight" Value="Bold" />
              <Setter Property="Foreground" Value="{Binding AgentState.SubState, Converter={StaticResource subStateToColorConverter}}" />
              <Setter Property="Background" Value="{Binding AgentState.State, Converter={StaticResource stateToBackgroundColorConverter}}" />
              <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
            </Style.Setters>
          </Style>
        </toolkit:DataGrid.RowStyle>

So, the Converter does the work. 所以,转换器完成了工作。 It reads the value of the State object (which is a child object on my AgentState object which is what the grid is binding to; it's binding to a collection of AgentState objects). 它读取State对象的值(它是我的AgentState对象上的子对象,它是网格绑定到的对象;它绑定到AgentState对象的集合)。 The converter reads the value of the state and returns a string representation of a color for the grid to use to set for the row. 转换器读取状态的值并返回用于为行设置的网格颜色的字符串表示形式。

Hope that helps. 希望有所帮助。

Have you tried CollectionViewSource Class ? 你试过CollectionViewSource Class吗?

See Here 看这里

and how to filter with it see below post 以及如何过滤它看到下面的帖子

Filtering With CollectionViewSource 使用CollectionViewSource过滤

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM