开发者

Loop through rows in Silverlight DataGrid

I have a feeling i'm missing something obvious here, but i 开发者_Go百科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.


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.

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.

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
  }

In my XAML I declare it like this:

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

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>

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.

Hope that helps.


Have you tried CollectionViewSource Class ?

See Here

and how to filter with it see below post

Filtering With CollectionViewSource

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜