Loop through rows in Silverlight DataGrid

故事扮演 提交于 2020-01-02 07:09:08

问题


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.


回答1:


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.




回答2:


Have you tried CollectionViewSource Class ?

See Here

and how to filter with it see below post

Filtering With CollectionViewSource



来源:https://stackoverflow.com/questions/4645914/loop-through-rows-in-silverlight-datagrid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!