2

I have bound an Observable Collection to a xamDataGrid of infragistics in my MVVM-light VM. In the UI I have my grid and a button and when I click on the button I want to perform an action that updates information. This action takes quite a while and I want the data to be updated in the order displayed on the GUI. However the Observable collection is not sorted and instead, it always updates on the unsorted list. Is there a way to get the sorted list in my VM?

VM:

 public class SystemInformation : ViewModelBase
 {
    private ObservableCollection<Site> _activeSites;

    private RelayCommand _updateAllCommand;

    /// <summary>
    /// Initializes a new instance of the SystemInformation class.
    /// </summary>
    public SystemInformation() : base()
    {
        ActiveSites = new ObservableCollection<Site>();
    }


    public ObservableCollection<Site> ActiveSites
    {
        get
        {
            return _activeSites;
        }
        set
        {
            Set("ActiveSites", ref _activeSites, value);
        }
    }

    public RelayCommand UpdateAllCommand
    {
        get
        {
            return _updateAllCommand
              ?? (_updateAllCommand= new RelayCommand(
                () =>
                {
                        try
                        {
                            foreach (var site in ActiveSites)
                            {
                             // Update data
                            }
                        }
                        catch (Exception ex)
                        {
                           //Exception handling
                        }
                    });
                }
             ));
        }
    }
}

}

View:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="5"/>
        <RowDefinition Height="*"/>
        <RowDefinition Height="5"/>
        <RowDefinition Height="auto"/>
        <RowDefinition Height="5"/>
    </Grid.RowDefinitions>

    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="5"/>
        <ColumnDefinition Width="auto"/>
        <ColumnDefinition Width="5"/>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="5"/>
    </Grid.ColumnDefinitions>

    <igDP:XamDataGrid  x:Name="grdSysinfo" Grid.Row="1" Grid.Column="3"  DataSource="{Binding ActiveSites, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged }" />
     <Button Command="{Binding UpdateAllCommand}" CommandParameter="All" Grid.Row="3" Grid.Column="1">Load All</Button>
</Grid>
  • try looking here - http://stackoverflow.com/questions/16956251/sort-a-wpf-datagrid-programmatically – Dave S Feb 23 '17 at 10:57
  • That's the other way around. They want to adjust their GUI in their vm. I want the collection in my vm to be updated according to the changes happened in my GUI. Unless I am totally missing something here. – Benjamin Van den Broeck Feb 23 '17 at 11:05

1 Answers1

1

The grid doesn't sort the actual source collection, it sorts a view of it.

If you want to be able to get the items in the order that they appear in the UI, you could bind to an ICollectionView property:

public class SystemInformation : ViewModelBase
{
    private ObservableCollection<Site> _activeSites;
    private RelayCommand _updateAllCommand;

    public SystemInformation() : base()
    {
        ActiveSites = new ObservableCollection<Site>();
        View = CollectionViewSource.GetDefaultView(ActiveSites);
    }

    public System.ComponentModel.ICollectionView View { get; private set; } //<-- bind to this one

    public ObservableCollection<Site> ActiveSites
    {
        get
        {
            return _activeSites;
        }
        set
        {
            Set("ActiveSites", ref _activeSites, value);
        }
    }

    public RelayCommand UpdateAllCommand
    {
        get
        {
            return _updateAllCommand
              ?? (_updateAllCommand = new RelayCommand(() => 
              {
                  foreach (var site in View.OfType<Site>())
                  {
                      //---
                  }
              }));
        }
    }
}

<igDP:XamDataGrid ... DataSource="{Binding View}" />
mm8
  • 163,881
  • 10
  • 57
  • 88
  • I implemented it like this, but it still does not give me the sorted collection. Perhaps this might be infragistics related? – Benjamin Van den Broeck Feb 24 '17 at 09:02
  • Do you mean the Site objects are not in the correct order when you enumerate View.OfType? – mm8 Feb 24 '17 at 09:13
  • 1
    Jus found my problem. It was indeed infragistics specific. The xamdatagrid does not use the collectionview by default! http://help.infragistics.com/Help/Doc/WPF/2012.2/CLR4.0/html/xamDataGrid_External_Sorting.html Thanks for the help! – Benjamin Van den Broeck Feb 24 '17 at 09:14