.net - 仅在 XAML 中更改 ListView 排序属性/方向

标签 .net wpf xaml sorting collectionviewsource

我有一个简单的ListView并希望按数字或字母顺序,升序或降序对内容进行排序。选择来自下拉框。我知道我可以使用 CollectionViewSource实现排序,但我怎样才能动态改变 SortDescription 或方向?

更新:

好的,所以我已经像这样设置了我的 CVS,viewModel 是 ListView 当前绑定(bind)的。我要求将 PropertyName 绑定(bind)到当前选定的组合框项的属性 PropertyName .组合框绑定(bind)到一个自定义列表,该列表公开我想要排序的属性名。

它提示我试图使用的 PropertyName:

A 'Binding' cannot be set on the 'PropertyName' property of type 'SortDescription'. A 'Binding' can only be set on a DependencyProperty of a DependencyObject.


    <CollectionViewSource Source="{StaticResource viewModel.ListValues}" x:Key="cvs">
        <CollectionViewSource.SortDescriptions>
            <scm:SortDescription PropertyName="{Binding Path=SortPropertyName, Source=comboSort}"/>
        </CollectionViewSource.SortDescriptions>
    </CollectionViewSource>

    <ListView ItemsSource="{Binding Source={StaticResource cvs}}"  />

最佳答案

你可以在你的 View 模型后面的代码中完成这一切

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

编辑

这是您的 View 模型的一个小示例
<ComboBox ItemsSource="{Binding viewmodel.YourDataForComboboxCollection, Mode=OneWay}"
          SelectedItem="{Binding viewmodel.SelectedCombobox}" />

一个小 View 模型
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;
  }
}

希望这可以帮助

关于.net - 仅在 XAML 中更改 ListView 排序属性/方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8331401/

相关文章:

c# - 如何重新查询单个 RoutedCommand 的 'CanExecute'?

asp.net - Visual Basic最大文件大小错误

c# - 锁变量应该声明为 volatile 吗?

c# - 我可以控制行为的产生吗?

c# - UWP 手动绑定(bind)处于非事件状态/变灰

c# - LINQ to Entities 查询中的 Include()

WPF datagrid 单元格颜色取决于先前的单元格值

c# - 文本框上的 XAML 控件模板,文本框上的数据触发器

wpf - WPF 布局面板(例如网格)的过度嵌套在计算上是否昂贵?

c# - 我的自定义值转换器导致 XAML 验证工具失败