Scrolling rows in a Silverlight DataGrid

I have the feeling that I missed something obvious here, but I cannot find a way to iterate in the DataGrids DataGridRow collection. I have a grid that has an itemssource collection of my set of classes. I am trying to iterate through the lines and select any lines that match a certain condition, but cannot see for me how it looks.

+3
source share
2 answers

You do not want to go through the grid. Old-fashioned WinForms is thinking about this. Grids in WPF and Silverlight were redesigned with MVVM in mind; with separation of problems. Instead of manipulating the grid, you work directly with your objects tied to the grid. Thus, the grid just becomes a representation problem. His duty is to read objects and display information based on the data in these objects.

Instead, you should attach properties to the object you are attached to and have grid styles for color / font / etc. based on these parameters. To do this, you need to create an IValueConverter.

Here is an example of the converter that I have in 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 this as follows:

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

In a datagrid declaration in XAML, I indicate the use of a converter for a 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>

, . State ( AgentState, , AgentState). , .

, .

+2

CollectionViewSource?

.

, .

CollectionViewSource

0

Source: https://habr.com/ru/post/1784499/


All Articles