WPF DataGrid CustomSort for each Column

后端 未结 10 1469
庸人自扰
庸人自扰 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条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-01 02:17

    here is some extention to @trilson86 ICustomeSorter

    that make the Sorter more generic to use

    NumericComparer based on this resource

    http://www.codeproject.com/Articles/11016/Numeric-String-Sort-in-C

    public class GenericNumericComparer : ICustomSorter
    {
        private PropertyInfo _propertyInfo;
        private Type _objectType;
    
        public string SortMemberPath { get; set; }
    
        private readonly NumericComparer _comparer = new NumericComparer();
    
        public Type ObjectType
        {
            get { return _objectType; }
            set
            {
                _objectType = value;
    
                if (_objectType != null) _propertyInfo = ObjectType.GetProperty(SortMemberPath);
            }
        }
    
        private int CompareHelper(object x, object y)
        {
            if (_propertyInfo != null)
            {
                var value1 = _propertyInfo.GetValue(x);
                var value2 = _propertyInfo.GetValue(y);
    
                return _comparer.Compare(value1, value2);
            }
    
            return 0;
        }
    
        public int Compare(object x, object y)
        {
            var i = CompareHelper(x, y);
    
            if (SortDirection == ListSortDirection.Ascending)
                return i;
    
            return i*-1;
        }
    
        public ListSortDirection SortDirection { get; set; }
    }
    

提交回复
热议问题