2011-11-30 35 views
5

Tôi có một đơn giản ListView và muốn sắp xếp các nội dung theo số hoặc theo thứ tự bảng chữ cái, tăng dần hoặc giảm dần. Sự lựa chọn đến từ một hộp thả xuống. Tôi hiểu rằng tôi có thể sử dụng CollectionViewSource để đạt được phân loại nhưng làm cách nào tôi có thể thay đổi SortDescription hoặc chỉ đường khi đang di chuyển?Thay đổi kiểu xem danh sách Thuộc tính/Chỉ trong XAML chỉ

Cập nhật:

Ok vì vậy tôi đã thiết lập CVS của tôi như vậy, ViewModel là những gì các ListView hiện đang bị ràng buộc để. Tôi yêu cầu PropertyName bị ràng buộc với thuộc tính của mục combo box hiện đang được chọn PropertyName. Hộp kết hợp được liên kết với danh sách tùy chỉnh hiển thị tên thuộc tính mà tôi muốn sắp xếp.

Nó phàn nàn về PropertyName mà im cố gắng sử dụng:

A 'Binding' không thể được đặt trên bất động sản 'PropertyName' kiểu 'SortDescription'. Chỉ có thể đặt 'Binding' trên DependencyProperty củ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}}" /> 
+0

bạn có thể loại bỏ các mô tả loại cũ sau đó thêm các mô tả loại mới (với Comparer khác nhau) và cuối cùng là gọi refresh trên CollectionViewSource – punker76

+0

Tôi muốn để làm điều này trong XAML chỉ, tức là không có mã phía sau (mã trong ViewModel là chấp nhận được) – Chris

+0

có rồi làm điều đó, hộp thả xuống của bạn liên kết với một thuộc tính tại viewmodel của bạn và sau khi thay đổi tha lựa chọn bạn có thể thay đổi CollectionViewSource của bạn (listvie của bạn w liên kết với CollectionViewSource) – punker76

Trả lời

2

bạn có thể này tất cả cùng mã sau trong viewmodel của bạn

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

đây là một ví dụ nhỏ cho mô hình quan điểm của bạn

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

một ít vie wmodel

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

hy vọng điều này giúp

+0

Làm thế nào để phát hiện sự thay đổi hộp combo trong ViewModel để cập nhật CVS? – Chris

+0

xem câu trả lời đã chỉnh sửa, hy vọng điều này sẽ hữu ích – punker76

2

Bạn cũng có thể đặt điều này vào một hành vi, thêm tài sản khác để ràng buộc vào để tự động thiết lập các hướng mô tả sắp xếp, nhưng giải pháp này chỉ có tác dụng đối với sắp xếp theo một tài sản. Nó chắc chắn có thể được mở rộng để làm việc nhiều hơn.

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> 

Hành vi:

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); 
    } 
} 
Các vấn đề liên quan