Beispiel #1
0
        private void FilterLoadFunction(object args)
        {
            RoutedEventArgs      routedEventArgs = args as RoutedEventArgs;
            DataGridColumnHeader columnHeader    = (routedEventArgs.OriginalSource as ToggleExtend).GetParent <DataGridColumnHeader>();
            DataGridColumn       dataGridColumn  = columnHeader.Column;

            OptionColumnInfo = new OptionColumnInfo(dataGridColumn, DataGridExtend);

            RoutedEventArgs routedEvent = new RoutedEventArgs(DataGridExtend.InitFilterColumnEvent, OptionColumnInfo.PropertyPath);

            DataGridExtend.RaiseEvent(routedEvent);

            ObservableCollection <FilterKeyValue> filter = new ObservableCollection <FilterKeyValue>();

            if (FilteredItemsSource == null)
            {
                FilteredItemsSource = new ObservableCollection <FilterKeyValue>();
                foreach (var item in DataGridExtend.FilteredItemsSource)
                {
                    if (item is ICateoryFilter cateory)
                    {
                        FilterKeyValue filterKeyValue = new FilterKeyValue();
                        filterKeyValue.DiaplayValue = cateory.FilterColumnValue?.ToString();
                        filterKeyValue.ID           = cateory.ID;
                        FilteredItemsSource.Add(filterKeyValue);
                    }
                }
            }
            else
            {
                foreach (var item in DataGridExtend.FilteredItemsSource)
                {
                    if (item is ICateoryFilter cateory)
                    {
                        if (FilteredItemsSource.Where(p => p.ID == cateory.ID).Count() > 0)
                        {
                            FilterKeyValue filterKeyValue = FilteredItemsSource.Where(p => p.ID == cateory.ID).ToList()[0];
                            filterKeyValue.DiaplayValue = cateory.FilterColumnValue?.ToString();
                        }
                        else
                        {
                            FilterKeyValue filterKeyValue = new FilterKeyValue();
                            filterKeyValue.DiaplayValue = cateory.FilterColumnValue?.ToString();
                            FilteredItemsSource.Add(filterKeyValue);
                        }
                    }
                }
            }


            foreach (var item in FilteredItemsSource)
            {
                if (filter.Where(p => p.DiaplayValue == item.DiaplayValue).ToList().Count <= 0)
                {
                    filter.Add(item);
                }
            }
            CategoryFilter = filter;
        }
        void ColumnFilterControl_Loaded(object sender, RoutedEventArgs e)
        {
            DataGridColumn       column    = null;
            DataGridColumnHeader colHeader = null;

            UIElement parent = (UIElement)VisualTreeHelper.GetParent(this);

            while (parent != null)
            {
                parent = (UIElement)VisualTreeHelper.GetParent(parent);
                if (colHeader == null)
                {
                    colHeader = parent as DataGridColumnHeader;
                }

                if (Grid == null)
                {
                    Grid = parent as DataGridExtend;
                }
            }

            if (colHeader != null)
            {
                column = colHeader.Column;
            }

            CanUserFreeze         = Grid.CanUserFreeze;
            CanUserGroup          = Grid.CanUserGroup;
            CanUserSelectDistinct = Grid.CanUserSelectDistinct;

            FilterColumnInfo = new OptionColumnInfo(column, Grid);

            if (column != null)
            {
                object oCanUserFreeze = column.GetValue(ColumnConfiguration.CanUserFreezeProperty);
                if (oCanUserFreeze != null)
                {
                    CanUserFreeze = (bool)oCanUserFreeze;
                    Grid.FreezeColumn(FilterColumnInfo.Column);
                }


                object oCanUserGroup = column.GetValue(ColumnConfiguration.CanUserGroupProperty);
                if (oCanUserGroup != null)
                {
                    CanUserGroup = (bool)oCanUserGroup;
                    if (!string.IsNullOrWhiteSpace(FilterColumnInfo.PropertyPath))
                    {
                        Grid.AddGroup(FilterColumnInfo.PropertyPath);
                    }
                }


                object oCanUserSelectDistinct = column.GetValue(ColumnConfiguration.CanUserSelectDistinctProperty);
                if (oCanUserSelectDistinct != null)
                {
                    CanUserSelectDistinct = (bool)oCanUserSelectDistinct;
                }
            }
            if (Grid.FilterType == null)
            {
                return;
            }



            Grid.RegisterOptionControl(this);

            FilterOperations.Clear();
            if (FilterColumnInfo.PropertyType != null)
            {
                if (TypeHelper.IsNumbericType(FilterColumnInfo.PropertyType) && TypeHelper.IsStringType(FilterColumnInfo.PropertyConvertType))
                {
                    FilterOperations.Add(new FilterOperationItem(Enums.FilterOperation.Equals, "=", "/Jib.WPF.Controls;component/Images/Equal.png"));
                    CalcControlVisibility(ColumnType.ConvertString);
                }
                else
                {
                    if (TypeHelper.IsStringType(FilterColumnInfo.PropertyType) || TypeHelper.IsStringType(FilterColumnInfo.PropertyConvertType))
                    {
                        FilterOperations.Add(new FilterOperationItem(Enums.FilterOperation.Contains, "Contains", "/Jib.WPF.Controls;component/Images/Contains.png"));
                        FilterOperations.Add(new FilterOperationItem(Enums.FilterOperation.StartsWith, "Starts With", "/Jib.WPF.Controls;component/Images/StartsWith.png"));
                        FilterOperations.Add(new FilterOperationItem(Enums.FilterOperation.EndsWith, "Ends With", "/Jib.WPF.Controls;component/Images/EndsWith.png"));
                        CalcControlVisibility(ColumnType.String);
                    }
                    FilterOperations.Add(new FilterOperationItem(Enums.FilterOperation.Equals, "=", "/Jib.WPF.Controls;component/Images/Equal.png"));
                    if (TypeHelper.IsNumbericType(FilterColumnInfo.PropertyType))
                    {
                        FilterOperations.Add(new FilterOperationItem(Enums.FilterOperation.GreaterThan, ">", "/Jib.WPF.Controls;component/Images/GreaterThan.png"));
                        FilterOperations.Add(new FilterOperationItem(Enums.FilterOperation.GreaterThanEqual, ">=", "/Jib.WPF.Controls;component/Images/GreaterThanEqual.png"));
                        FilterOperations.Add(new FilterOperationItem(Enums.FilterOperation.LessThan, "<", "/Jib.WPF.Controls;component/Images/LessThan.png"));
                        FilterOperations.Add(new FilterOperationItem(Enums.FilterOperation.LessThanEqual, "<=", "/Jib.WPF.Controls;component/Images/GreaterThanEqual.png"));
                        FilterOperations.Add(new FilterOperationItem(Enums.FilterOperation.NotEquals, "!=", "/Jib.WPF.Controls;component/Images/NotEqual.png"));
                        CalcControlVisibility(ColumnType.Num);
                    }
                    if (TypeHelper.IsBoolType(FilterColumnInfo.PropertyType) || TypeHelper.IsBoolType(FilterColumnInfo.PropertyConvertType))
                    {
                        CalcControlVisibility(ColumnType.Bool);
                    }
                    if (TypeHelper.IsDateTimeType(FilterColumnInfo.PropertyType))
                    {
                        FilterOperations.Add(new FilterOperationItem(Enums.FilterOperation.GreaterThan, ">", "/Jib.WPF.Controls;component/Images/GreaterThan.png"));
                        FilterOperations.Add(new FilterOperationItem(Enums.FilterOperation.GreaterThanEqual, ">=", "/Jib.WPF.Controls;component/Images/GreaterThanEqual.png"));
                        FilterOperations.Add(new FilterOperationItem(Enums.FilterOperation.LessThan, "<", "/Jib.WPF.Controls;component/Images/LessThan.png"));
                        FilterOperations.Add(new FilterOperationItem(Enums.FilterOperation.LessThanEqual, "<=", "/Jib.WPF.Controls;component/Images/GreaterThanEqual.png"));
                        FilterOperations.Add(new FilterOperationItem(Enums.FilterOperation.NotEquals, "!=", "/Jib.WPF.Controls;component/Images/NotEqual.png"));
                        CalcControlVisibility(ColumnType.Date);
                    }
                }

                if (FilterOperations.Count > 0)
                {
                    SelectedFilterOperation = FilterOperations[0];
                }
            }

            if (FilterColumnInfo != null && FilterColumnInfo.IsValid)
            {
                foreach (var i in DistinctPropertyValues.Where(i => i.IsChecked))
                {
                    i.IsChecked = false;
                }
                DistinctPropertyValues.Clear();
                FilterText = string.Empty;
                _boundColumnPropertyAccessor = null;

                if (!string.IsNullOrWhiteSpace(FilterColumnInfo.PropertyPath))
                {
                    if (FilterColumnInfo.PropertyPath.Contains('.'))
                    {
                        throw new ArgumentException(string.Format("This version of the grid does not support a nested property path such as '{0}'.  Please make a first-level property for filtering and bind to that.", FilterColumnInfo.PropertyPath));
                    }

                    this.Visibility = System.Windows.Visibility.Visible;
                    ParameterExpression arg = System.Linq.Expressions.Expression.Parameter(typeof(object), "x");
                    System.Linq.Expressions.Expression expr = System.Linq.Expressions.Expression.Convert(arg, Grid.FilterType);
                    expr = System.Linq.Expressions.Expression.Property(expr, Grid.FilterType, FilterColumnInfo.PropertyPath);
                    System.Linq.Expressions.Expression conversion = System.Linq.Expressions.Expression.Convert(expr, typeof(object));
                    _boundColumnPropertyAccessor = System.Linq.Expressions.Expression.Lambda <Func <object, object> >(conversion, arg).Compile();

                    if (_boundColumnPropertyAccessor != null)
                    {
                        if (DistinctPropertyValues.Count == 0)
                        {
                            //List<object> result = new List<object>();
                            //foreach (var i in Grid.ItemsSource)
                            //{
                            //    object value = _boundColumnPropertyAccessor(i);
                            //    if (value != null)
                            //    {
                            //        if (result.Where(o => o.ToString() == value.ToString()).Count() == 0)
                            //            result.Add(value);
                            //    }
                            //    else if (FilterColumnInfo.PropertyType == typeof(bool?) && FilterColumnInfo.IsSpecialState)
                            //    {
                            //        if (result.Where(o => o == value).Count() == 0)
                            //            result.Add(value);
                            //    }

                            //}
                            //try
                            //{
                            //    result.Sort();
                            //}
                            //catch
                            //{
                            //    if (System.Diagnostics.Debugger.IsLogging())
                            //        System.Diagnostics.Debugger.Log(0, "Warning", "There is no default compare set for the object type");
                            //}

                            //if (result.Count > 0)
                            //{
                            //    StringBuilder sb = new StringBuilder();
                            //    foreach (var obj in result)
                            //    {
                            //        var item = new CheckboxComboItem()
                            //        {
                            //            Description = GetFormattedValue(obj),
                            //            Tag = obj,
                            //            IsChecked = true
                            //        };
                            //        item.PropertyChanged += new PropertyChangedEventHandler(filter_PropertyChanged);
                            //        DistinctPropertyValues.Add(item);
                            //        sb.AppendFormat("{0}{1}", sb.Length > 0 ? "," : "",item.Description );
                            //    }

                            //    FilterText = sb.ToString();
                            //}
                            //else
                            //{
                            //    FilterText = string.Empty;
                            //}
                        }
                    }
                }
                else
                {
                    this.Visibility = System.Windows.Visibility.Collapsed;
                }
                object oDefaultFilter = column.GetValue(ColumnConfiguration.DefaultFilterProperty);
                if (oDefaultFilter != null)
                {
                    FilterText = (string)oDefaultFilter;
                }
            }
        }