2011-11-30 3 views
5

나는 간단한 ListView을 가지고 오름차순 또는 내림차순, 숫자 또는 알파벳 순으로 내용을 정렬 할. 선택 항목은 드롭 다운 상자에서옵니다. 내가 CollectionViewSource 정렬을 달성 할 수 있지만 어떻게 SortDescription 또는 방향을 즉시 변경할 수 있습니다 이해합니까?변경 목록보기 정렬 재산권/방향은

업데이트 : 내가 지금처럼 내 CVS는, 뷰 모델리스트 뷰가 현재 바인딩되는 것입니다 설정을 가지고 있으므로

좋아. 현재 선택된 콤보 상자 항목의 속성 PropertyName에 바인딩 할 PropertyName이 필요합니다. 콤보 상자는 정렬하려는 propertyname을 표시하는 사용자 지정 목록에 바인딩됩니다. 그것은 사용하기 메신저 프롭퍼티를 뿌려

:

A '바인딩은'유형 'SortDescription'의 'PROPERTYNAME'속성에 설정할 수 없습니다. 'Binding'은 DependencyObject의 DependencyProperty 에서만 설정할 수 있습니다.

<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

당신이 다음 (다른 비교 자에) 새로운 종류의 설명을 추가 이전 정렬 설명을 제거하고 마지막으로 내가 원하는 CollectionViewSource – punker76

+0

에 새로 고침을 호출 할 수 있습니다 (뷰 모델에서 코드 가능) – Chris

+0

예 다음은 상자 아래로 드롭이 당신의 ViewModel에서 속성을 결합하고 당신이 당신의 CollectionViewSource을 변경할 수 있습니다 그쪽으로 선택을 변경 한 후 수행합니다 (listvie 뒤에 어떤 코드를 즉 없습니다 만 XAML에서이 작업을 수행하는 w는 CollectionViewSource에 바인딩합니다.) – punker76

답변

2

할 수 있습니다

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

을위한 작은 예입니다 모든 뷰 모델에 뒤에 코드를이 작은 벌 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; 
    } 
} 

희망이 동적으로 정렬 설명 방향을 설정하기 위해 결합하는 다른 속성을 추가,

+0

CVS를 업데이트하려면 ViewModel의 콤보 상자 변경을 어떻게 감지합니까? – Chris

+0

수정 된 답변보기, 이것이 도움이 되길 바랍니다. – punker76

2

당신은 또한 행동에이를 넣을 수 있습니다하지만이 솔루션은 하나 개의 속성에 의해 정렬 작동합니다. 그것은 확실히 더 많은 것을 위해 일할 수 있도록 확장 될 수 있습니다.

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