WPF DataGrid CustomSort for each Column

后端 未结 10 1494
庸人自扰
庸人自扰 2020-12-01 01:56

I have a WPF DataGrid bound to a CollectionViewSource that encapsulates an ObservableCollection. This CollectionViewSource has two main objectives:

1) To group each

10条回答
  •  猫巷女王i
    2020-12-01 02:14

    I've learned a lot from this question, and here I share a way of doing it.
    This method does not modify xaml while conforming to mvvm.
    I need to load the file path to DataGrid, which can be sorted like Windows Explorer.
    there have some files:

    E:\Test\Item_1.txt
    E:\Test\Item_04.txt
    E:\Test\Item_5.txt
    E:\Test\Item_10.txt

    Note that I have sorted by file name by Windows Explorer.
    I think you've found that the file sort used by Explorer is not a simple string sort.
    It uses the win32 api StrCmpLogicalW in shlwapi.dll
    We need to implement the IComparable(Non-generic) interface for the sorted properties.
    For less code, I used Prism.Mvvm.BindableBase, an INotifyPropertyChanged implementation.
    Code like this:

        /// 
        /// Data Model
        /// 
        public class ListItemModel : BindableBase
        {
    
            private FilePath filePath;
            public FilePath FilePath
            {
                get { return filePath; }
                set { SetProperty(ref filePath, value); }
            }
            
            /// Other properties.
            /// ....
        }
    
        /// 
        /// wrapper of filepath
        /// 
        public class FilePath : IComparable
        {
            private string filePath;
    
            [DllImport("shlwapi.dll", CharSet = CharSet.Unicode)]
            private static extern int StrCmpLogicalW(string psz1, string psz2);
    
            public FilePath(string filePath)
            {
                this.filePath = filePath;
            }
    
            /// 
            /// Implicit type conversion.
            /// 
            /// 
            public static implicit operator string(FilePath x)
            {
                return x.filePath;
            }
    
            public static implicit operator FilePath(string x)
            {
                return new FilePath(x);
            }
    
            /// 
            /// override for datagrid display.
            /// 
            /// 
            public override string ToString()
            {
                return filePath;
            }
    
            /// 
            /// Implement the interface IComparable for Custom sorting.
            /// 
            /// 
            /// 
            public int CompareTo(object obj)
            {
                if (obj is FilePath other)
                    return StrCmpLogicalW(filePath, other.filePath);
                return 1;
            }
        }
    

    XAML code:

                    
            
                
                    
                
            
    

    In summary, I encapsulated the original string properties,
    You can also encapsulate your custom properties for sorting,
    just override ToString for display and implemented CompareTo for sorting.

提交回复
热议问题