你可以在你的视图模型后面的代码中完成这一切
// in your view model
private void ChangeSorting () {
var collView = CollectionViewSource.GetDefaultView(ListValues);
collView.SortDescriptions.Clear();
// do this one
collView.SortDescriptions.Add(new SortDescription("YourPropertyName", ListSortDirection.Ascending));
// or this one
collView.SortDescriptions.Add(new SortDescription("YourOtherPropertyName", ListSortDirection.Descending));
collView.Refresh();
}
public ICollectionView ListValuesCollectionViewSource
{
get {
return collView;
}
}
<ListView ItemsSource="{Binding viewModel.ListValuesCollectionViewSource}" />
编辑
这是您的视图模型的一个小示例
<ComboBox ItemsSource="{Binding viewmodel.YourDataForComboboxCollection, Mode=OneWay}"
SelectedItem="{Binding viewmodel.SelectedCombobox}" />
一个小视图模型
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows;
using System.Windows.Data;
namespace YourNameSpace
{
public class ViewModel : INotifyPropertyChanged
{
public static readonly DependencyProperty SelectedComboboxProperty =
DependencyProperty.Register("SelectedCombobox", typeof(YourDataForCombobox), typeof(ViewModel), new PropertyMetadata(default(YourDataForCombobox), new PropertyChangedCallback(SelectedComboboxCallback)));
private static void SelectedComboboxCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e) {
var vm = sender as ViewModel;
if (vm != null && e.NewValue != null && e.NewValue != e.OldValue) {
vm.ChangeSorting(e.NewValue);
}
}
public ViewModel() {
this.YourDataForComboboxCollection = new ObservableCollection<YourDataForCombobox>();
}
private void ChangeSorting(YourDataForCombobox newValue) {
this.yourCollectionView.SortDescriptions.Clear();
this.yourCollectionView.SortDescriptions.Add(new SortDescription(newValue.PropertyName, newValue.Sorting));
this.yourCollectionView.Refresh();
}
private IObservableCollection yourDataForComboboxCollection;
public IObservableCollection YourDataForComboboxCollection {
get { return this.yourDataForComboboxCollection; }
set {
this.yourDataForComboboxCollection = value;
this.RaisePropertyChanged("YourDataForComboboxCollection");
}
}
public YourDataForCombobox SelectedCombobox {
get { return (YourDataForCombobox)GetValue(SelectedComboboxProperty); }
set { SetValue(SelectedComboboxProperty, value); }
}
private IObservableCollection yourCollection;
private ICollectionView yourCollectionView;
public ICollectionView YourCollectionView {
get { return this.GetCollectionView(); }
}
private ICollectionView GetCollectionView() {
if (this.yourCollection == null) {
this.yourCollection = new ObservableCollection<YourDataForCollection>();
this.yourCollectionView = CollectionViewSource.GetDefaultView(this.yourCollection);
// initial sorting
this.ChangeSorting(null);
}
return this.yourCollectionView;
}
private void RaisePropertyChanged(string property) {
var eh = this.PropertyChanged;
if (eh != null) {
eh(this, new PropertyChangedEventArgs(property));
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
}
希望这可以帮助