2014-06-10 22 views
5

Tôi mới sử dụng WPF và tôi đang gặp khó khăn khi cố gắng sắp xếp một CollectionViewSource với một loại tùy chỉnh. Đây là tình hình:CollectionViewSource với tùy chỉnh sắp xếp

Tôi có một SearchView mà được gọi với một tham số mà trở nên thật DataContext như vậy:

mainView.SetGlobalOverlay(New SearchView With {.DataContext = interventionViewModel}) 

Trong SearchView.xaml, tôi sau đó ràng buộc CollectionViewSource để bộ sưu tập:

<CollectionViewSource x:Key="UnitsCollection" 
          Filter="UnitsCollection_Filter" 
          Source="{Binding Path=Units}" /> 

Bây giờ, tôi đã có giao diện IComparer được triển khai trong một lớp dùng chung khác. So sánh này được sử dụng trên một ListCollectionView ở một nơi khác trong mã nguồn và hoạt động tốt. Bây giờ, làm cách nào tôi có thể sử dụng lại bộ so sánh này trên một CollectionViewSource?

Trả lời

11

Để sử dụng bộ sắp xếp tùy chỉnh cho CollectionViewSource, bạn phải đợi đến khi số ItemsControl (ví dụ: hộp danh sách) được tải; sau đó bạn có thể lấy số ListCollectionView bằng cách sử dụng thuộc tính View của CollectionViewSource.

Như minh hoạ, đây là một ví dụ nhỏ mà sẽ hiển thị một danh sách các số nguyên theo hai cách khác nhau: phía trên hộp danh sách áp dụng một trật tự tùy chỉnh sắp xếp, trong khi các hộp danh sách thấp hơn là không được phân loại:

screen shot

MainWindow.xaml:

<Window x:Class="WpfApplication27.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:clr="clr-namespace:System;assembly=mscorlib" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="MainWindow" Height="350" Width="300"> 
    <Window.Resources> 
     <CollectionViewSource x:Key="MyCollectionViewSource1" Source="{Binding RawData}" /> 
     <CollectionViewSource x:Key="MyCollectionViewSource2" Source="{Binding RawData}" /> 
    </Window.Resources> 
    <Grid> 
     <Grid.RowDefinitions> 
      <RowDefinition/> 
      <RowDefinition/> 
     </Grid.RowDefinitions> 
     <ListBox Grid.Row="0" Margin="5" Background="LightSkyBlue" 
       ItemsSource="{Binding Source={StaticResource MyCollectionViewSource1}}"/> 
     <ListBox Grid.Row="1" Margin="5" Background="LightYellow" 
       ItemsSource="{Binding Source={StaticResource MyCollectionViewSource2}}"/> 
    </Grid> 
</Window> 

MainWindow.xaml.cs:

using System.Collections; 
using System.Collections.ObjectModel; 
using System.Windows; 
using System.Windows.Data; 

namespace WpfApplication27 
{ 
    public partial class MainWindow : Window 
    { 
     public ObservableCollection<int> RawData { get; private set; } 

     public MainWindow() 
     { 
      RawData = new ObservableCollection<int> { 10, 222, 1, 333, 2, 777, 6 }; 

      InitializeComponent(); 

      DataContext = this;    

      this.Loaded += MainWindow_Loaded; 
     } 

     void MainWindow_Loaded(object sender, RoutedEventArgs e) 
     { 
      CollectionViewSource source = (CollectionViewSource)(this.Resources["MyCollectionViewSource1"]); 
      ListCollectionView view = (ListCollectionView)source.View; 
      view.CustomSort = new CustomSorter(); 
     } 
    } 

    // Sort by number of digits (descending), then by value (ascending) 
    public class CustomSorter : IComparer 
    { 
     public int Compare(object x, object y) 
     { 
      int digitsX = x.ToString().Length; 
      int digitsY = y.ToString().Length; 
      if (digitsX < digitsY) 
      { 
       return 1; 
      } 
      else if (digitsX > digitsY) 
      { 
       return -1; 
      } 
      return (int) x - (int) y; 
     } 
    } 
} 
+0

Làm thế nào tôi có thể làm cho nó hoạt động cho một ObservableCollection với tên tài sản? Tôi đang cố gắng làm một nền văn hóa bất biến như osrt như đã đề cập ở đây stackoverflow.com/questions/31332882/culture-specific-sorting-on-icollectionview –

Các vấn đề liên quan