通过在Silverlight的DataGrid行循环Silverlight、DataGrid

2023-09-06 07:25:46 作者:粉色的你

我有一种感觉,我缺少明显的东西在这里,但我不能找到一种方法来遍历DataGrid中DataGridRow集合。我有我的类集的集合的的ItemsSource的网格。我想遍历行,并强调符合一定的条件,但任何行不能为我的生活怎么看。

I have a feeling i'm missing something obvious here, but i can not find a way to iterate through a DataGrids DataGridRow collection. I have a grid which has an itemssource of a collection of my class set. 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.

推荐答案

您不想遍历网格。这是老斯库尔的WinForms思考。该网格在WPF和Silverlight已经过重新设计与MVVM考虑;与关注点分离。相反操纵网格,你被绑定到网格您的物品直接工作。因此,电网只是变成了presentation关注。其职责是基于在这些对象中的数据读取的对象和显示信息。

You don't want to iterate through the grid. That's old-skool WinForms thinking. The Grids in WPF and Silverlight have been redesigned with MVVM in mind; 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.

你想做的事,而不是什么是附加属性,你绑定到该对象并有网格设置样式颜色/字体/等,根据这些设置。要做到这一点,你需要创建的IValueConverter。

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.

下面是一个转换器我有一个WPF和Silverlight的DataGrid的例子:

Here is an example of a converter I have in a WPF and Silverlight datagrid:

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
  }

在我的XAML我宣布它是这样的:

In my XAML I declare it like this:

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

在数据网格的声明在XAML我指定的转换器使用情况DataGridRow:

In the datagrid declaration in XAML I specify the converter usage for the 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>

因此​​,转换器做的工作。它读取国家对象的值(这是对我的AgentState对象的子对象,而这正是网格结合;它绑定到AgentState对象的集合)。所述转换器读出状态的值,并返回字符串重的颜色的presentation为网格用来为行设置。

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). 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.

希望有所帮助。