仅在 XAML 中更改列表视图排序属性/方向

发布于 2024-12-19 00:21:48 字数 940 浏览 1 评论 0原文

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

更新:

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

它抱怨我尝试使用的 PropertyName:

无法在类型的“PropertyName”属性上设置“Binding” '排序描述'。只能在 DependencyProperty 上设置“Binding” 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}}"  />

I have a simple ListView and want to sort the contents numerically or alphabetically, ascending or descending. The choice comes from a drop down box. I understand I can use CollectionViewSource to achieve the sorting but how can I alter the SortDescription or direction on the fly?

Update:

Ok so I have setup my CVS like so, the viewModel is what the ListView is currently bound to. I require the PropertyName to be bound to the currently selected combo box item's property PropertyName. The combo box is bound to a custom list that expose the propertyname on which I want to sort.

It complains about the PropertyName that im attempting to use:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

追星践月 2024-12-26 00:21:48

你可以在你的视图模型的代码后面

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

希望这有帮助

you can this all at code behind in your viewmodel

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

EDIT

here is a little example for your view model

<ComboBox ItemsSource="{Binding viewmodel.YourDataForComboboxCollection, Mode=OneWay}"
          SelectedItem="{Binding viewmodel.SelectedCombobox}" />

a little viewmodel

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

hope this helps

两个我 2024-12-26 00:21:48

您还可以将其放入行为中,添加另一个要绑定的属性以动态设置排序描述方向,但此解决方案仅适用于按一个属性排序。它当然可以扩展到更多用途。

XAML:

    <CollectionViewSource x:Key="GroupedMeetingItems" Source="{Binding Items}" util:CollectionViewSourceBehavior.IsAscending="{Binding IsItemsAscending}">
        <CollectionViewSource.GroupDescriptions>
            <PropertyGroupDescription PropertyName="StartDateTime" Converter="{StaticResource DateTimeToDisplayDateConverter}" />
        </CollectionViewSource.GroupDescriptions>
        <CollectionViewSource.SortDescriptions>
            <scm:SortDescription PropertyName="StartDateTime" Direction="Descending"/>
        </CollectionViewSource.SortDescriptions>
    </CollectionViewSource> 

行为:

public static class CollectionViewSourceBehavior
{
    public static readonly DependencyProperty IsAscendingProperty =
        DependencyProperty.RegisterAttached(
            "IsAscending",
            typeof(bool),
            typeof(CollectionViewSourceBehavior),
            new UIPropertyMetadata(false, OnIsAscendingChanged));

    public static object GetIsAscending(FrameworkElement element)
    {
        return element.GetValue(IsAscendingProperty);
    }

    public static void SetIsAscending(FrameworkElement element, object value)
    {
        element.SetValue(IsAscendingProperty, value);
    }

    public static void OnIsAscendingChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
    {
        var collectionViewSource = dependencyObject as CollectionViewSource;
        if (collectionViewSource == null)
        {
            return;
        }

        var isAscending = e.NewValue as bool? == true;
        var newSortDescription = new SortDescription
            {
                Direction = isAscending ? ListSortDirection.Ascending : ListSortDirection.Descending,
                PropertyName = collectionViewSource.SortDescriptions.FirstOrDefault().PropertyName
            };
        collectionViewSource.SortDescriptions.Clear();
        collectionViewSource.SortDescriptions.Add(newSortDescription);
    }
}

You can also put this into a behavior, adding another property to bind to to dynamically set the sort description direction, but this solution only works for sorting by one property. It could certainly be expanded to work for more.

XAML:

    <CollectionViewSource x:Key="GroupedMeetingItems" Source="{Binding Items}" util:CollectionViewSourceBehavior.IsAscending="{Binding IsItemsAscending}">
        <CollectionViewSource.GroupDescriptions>
            <PropertyGroupDescription PropertyName="StartDateTime" Converter="{StaticResource DateTimeToDisplayDateConverter}" />
        </CollectionViewSource.GroupDescriptions>
        <CollectionViewSource.SortDescriptions>
            <scm:SortDescription PropertyName="StartDateTime" Direction="Descending"/>
        </CollectionViewSource.SortDescriptions>
    </CollectionViewSource> 

Behavior:

public static class CollectionViewSourceBehavior
{
    public static readonly DependencyProperty IsAscendingProperty =
        DependencyProperty.RegisterAttached(
            "IsAscending",
            typeof(bool),
            typeof(CollectionViewSourceBehavior),
            new UIPropertyMetadata(false, OnIsAscendingChanged));

    public static object GetIsAscending(FrameworkElement element)
    {
        return element.GetValue(IsAscendingProperty);
    }

    public static void SetIsAscending(FrameworkElement element, object value)
    {
        element.SetValue(IsAscendingProperty, value);
    }

    public static void OnIsAscendingChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
    {
        var collectionViewSource = dependencyObject as CollectionViewSource;
        if (collectionViewSource == null)
        {
            return;
        }

        var isAscending = e.NewValue as bool? == true;
        var newSortDescription = new SortDescription
            {
                Direction = isAscending ? ListSortDirection.Ascending : ListSortDirection.Descending,
                PropertyName = collectionViewSource.SortDescriptions.FirstOrDefault().PropertyName
            };
        collectionViewSource.SortDescriptions.Clear();
        collectionViewSource.SortDescriptions.Add(newSortDescription);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文