在 mvvm light 中获取已排序的可观察集合

Get sorted observable collection in mvvm light

我已将 Observable 集合绑定到我的 MVVM-light 虚拟机中的 xamDataGrid 基础设施。在 UI 中,我有我的网格和一个按钮,当我单击该按钮时,我想执行更新信息的操作。此操作需要相当长的时间,我希望数据按照 GUI 上显示的顺序更新。然而,Observable 集合没有排序,相反,它总是在未排序的列表上更新。有没有办法在我的 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
                        }
                    });
                }
             ));
        }
    }
}

}

查看:

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

网格不对实际源集合进行排序,它对它的视图进行排序。

如果您希望能够按照项目在 UI 中出现的顺序获取项目,您可以绑定到 ICollectionView 属性:

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}" />