我已将一个Observable Collection绑定到我的MVVM-light VM中的infragistics的xamDataGrid。在UI中我有我的网格和一个按钮,当我点击按钮时,我想执行一个更新信息的操作。此操作需要一段时间,我希望数据按GUI上显示的顺序更新。但是,Observable集合未排序,而是始终在未排序列表上更新。有没有办法在我的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
}
});
}
));
}
}
}
}
查看:
<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>
答案 0 :(得分:1)
网格没有对实际的源集合进行排序,它会对其进行排序。
如果您希望能够按照它们在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}" />