2011-11-30 12 views
5

मेरे पास एक साधारण ListView है और सामग्री संख्यात्मक या वर्णानुक्रम, आरोही या अवरोही को सॉर्ट करना चाहते हैं। पसंद एक ड्रॉप डाउन बॉक्स से आता है। मैं समझता हूं कि सॉर्टिंग प्राप्त करने के लिए मैं CollectionViewSource का उपयोग कर सकता हूं लेकिन मैं फ्लाई पर सॉर्ट डिस्क्रिप्शन या दिशा कैसे बदल सकता हूं?सूची दृश्य प्रकार बदलें XAML के भीतर संपत्ति/दिशा केवल

अद्यतन:

ठीक है तो मैं सेटअप है तो तरह मेरे सीवीएस, ViewModel क्या ListView वर्तमान में करने के लिए बाध्य किया जाता है। मुझे प्रॉपर्टीनाम को वर्तमान में चयनित कॉम्बो बॉक्स आइटम की संपत्ति PropertyName से बाध्य होने की आवश्यकता है। कॉम्बो बॉक्स एक कस्टम सूची से जुड़ा हुआ है जो उस संपत्ति नाम का पर्दाफाश करता है जिस पर मैं सॉर्ट करना चाहता हूं।

यह PropertyName कि उपयोग करने का प्रयास कर im के बारे में शिकायत:

ए 'बाध्यकारी' प्रकार 'SortDescription' के 'PropertyName' संपत्ति पर सेट नहीं किया जा सकता है। एक 'बाइंडिंग' केवल निर्भरता प्रोजेक्ट के निर्भरताप्रॉपर्टी पर सेट किया जा सकता है।

<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

आप पुराने प्रकार वर्णन निकाल सकते हैं तो नए तरह का विवरण (अलग comparer के साथ) जोड़ सकते हैं और अंत में CollectionViewSource – punker76

+0

मैं चाहता हूँ पर ताज़ा फोन इसे केवल एक्सएएमएल में करने के लिए, यानी कोई कोड पीछे नहीं है (व्यूमोडेल में कोड स्वीकार्य है) – Chris

+0

हां, फिर इसे करें, आपका ड्रॉप डाउन बॉक्स आपके व्यूमोडेल पर किसी संपत्ति से जुड़ा हुआ है और चयन बदलने के बाद आप अपना कलेक्शन व्यूसोर्स बदल सकते हैं (आपकी listvie w संग्रहViewSource को बाध्य करता है) – punker76

उत्तर

2

आप कर सकते हैं यह सब आपके 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}" /> 

संपादित

यहाँ

आपके विचार मॉडल

<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

सीवीएस अपडेट करने के लिए व्यूमोडेल में कॉम्बो बॉक्स परिवर्तन का पता कैसे लगा सकता हूं? – 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); 
    } 
} 
संबंधित मुद्दे