Trying to create custom filter for RadGridView

3 posts, 0 answers
  1. Mark
    Mark avatar
    3 posts
    Member since:
    Feb 2016

    Posted 02 Feb Link to this post

    I'm trying to create a custom filter for a RadGridView, but no matter what I do I get the following error.

     

    The value "(CategoryList Contains Allergies)" is not of type "Telerik.Windows.Data.IFilterDescriptor" and cannot be used in this generic collection.
    Parameter name: value

     

    The column that the filter is on contains a List<string> of possibly multiple category names in each row and the filter contains a distinct list of the categories.

     

    001.public partial class CategoryFilter : UserControl, IFilteringControl
    002.{
    003.    private GridViewBoundColumnBase _column;
    004. 
    005.    public delegate void BeginCategoryChangeEvent();
    006.    public event BeginCategoryChangeEvent BeginCategoryChange;
    007. 
    008.    public delegate void CategoryChangeEvent();
    009.    public event CategoryChangeEvent CategoryChange;
    010.    private CompositeFilterDescriptor _compositeFilter;
    011. 
    012. 
    013.    public class CategoryItem
    014.    {
    015.        public string CategoryName { get; set; }
    016.        public bool IsChecked { get; set; }
    017.    }
    018. 
    019.    private List<CategoryItem> MainCategoryList;
    020. 
    021.    /// <summary>
    022.    /// Gets or sets a value indicating whether the filtering is active.
    023.    /// </summary>
    024.    public bool IsActive
    025.    {
    026.        get { return (bool)GetValue(IsActiveProperty); }
    027.        set { SetValue(IsActiveProperty, value); }
    028.    }
    029. 
    030.    /// <summary>
    031.    /// Identifies the <see cref="IsActive"/> dependency property.
    032.    /// </summary>
    033.    public static readonly DependencyProperty IsActiveProperty =
    034.        DependencyProperty.Register(
    035.            "IsActive",
    036.            typeof(bool),
    037.            typeof(CategoryFilter),
    038.            new System.Windows.PropertyMetadata(false));
    039. 
    040. 
    041. 
    042. 
    043.    public bool this[string CategoryName]
    044.    {
    045.        get
    046.        {
    047.            CategoryItem item = MainCategoryList.FirstOrDefault(i => i.CategoryName == CategoryName);
    048. 
    049.            if (item == null)
    050.                throw new Exception("Category Name not found");
    051. 
    052.            return (item.IsChecked);
    053.        }
    054.        set
    055.        {
    056.            CategoryItem item = MainCategoryList.FirstOrDefault(i => i.CategoryName == CategoryName);
    057. 
    058.            if (item == null)
    059.                throw new Exception("Category Name not found");
    060. 
    061.            item.IsChecked = value;
    062.        }
    063.    }
    064. 
    065.    [TypeConverter(typeof(String))]
    066.    public List<string> Categories
    067.    {
    068.        get
    069.        {
    070.            return MainCategoryList.Where(i => i.IsChecked).Select(i => i.CategoryName).ToList();
    071.        }
    072.        set
    073.        {
    074.            foreach (CategoryItem item in MainCategoryList)
    075.            {
    076.                item.IsChecked = value.Contains(item.CategoryName);
    077.            }
    078.        }
    079.    }
    080. 
    081.    public System.Windows.Controls.ListBox CategoryList
    082.    {
    083.        get { return this.LstCategories; }
    084.    }
    085. 
    086.    public CategoryFilter()
    087.    {
    088.        InitializeComponent();
    089.    }
    090. 
    091.    public void Prepare(GridViewColumn column)
    092.    {
    093.        this._column = column as GridViewBoundColumnBase;
    094.        if (this._column == null)
    095.        {
    096.            return;
    097.        }
    098. 
    099.        List<string> catList = ((List<EducationChoices>)(column.DataControl.ItemsSource))
    100.            .SelectMany(l => l.CategoryList)
    101.            .Distinct()
    102.            .Where(l => (l ?? "")
    103.                .Trim() != "")
    104.            .OrderBy(c => c)
    105.            .ToList();
    106. 
    107.        MainCategoryList = catList
    108.                .Select(i => new CategoryItem() {CategoryName = i, IsChecked = false})
    109.                .ToList();
    110. 
    111.        LstCategories.ItemsSource = MainCategoryList;
    112.    }
    113. 
    114.    private void ChangeFilter(object sender, RoutedEventArgs e) {
    115.        BeginCategoryChange();
    116. 
    117.        if (_compositeFilter != null) {
    118.            _column.DataControl.FilterDescriptors.Remove(_compositeFilter);
    119.        }
    120.        _compositeFilter = new CompositeFilterDescriptor { LogicalOperator = FilterCompositionLogicalOperator.Or };
    121.        var dataMember = _column.DataMemberBinding.Path.Path;
    122. 
    123.        string Category = ((CheckBox)e.OriginalSource).Tag.ToString();
    124. 
    125.        foreach (var item in MainCategoryList) {
    126.            if (item.IsChecked) {
    127.                var filter = new FilterDescriptor(dataMember, FilterOperator.Contains, Category);
    128.                _compositeFilter.FilterDescriptors.Add(filter);
    129.            }
    130.        }
    131. 
    132.        if ((((CheckBox)e.OriginalSource).IsChecked ?? false) &&
    133.                    _compositeFilter.FilterDescriptors.All(f => ((FilterDescriptor)f).Value.ToString() != Category))
    134.        {
    135.            var filter = new FilterDescriptor(dataMember, FilterOperator.Contains, Category);
    136.            _compositeFilter.FilterDescriptors.Add(filter);
    137.        } else if (!(((CheckBox) e.OriginalSource).IsChecked ?? false) &&
    138.                   _compositeFilter.FilterDescriptors.Any(f => ((FilterDescriptor) f).Value.ToString() == Category))
    139.        {
    140.            _compositeFilter.FilterDescriptors.Remove(
    141.                _compositeFilter.FilterDescriptors.FirstOrDefault(
    142.                    f => ((FilterDescriptor) f).Value.ToString() == Category));
    143.        }
    144.       
    145. 
    146.        if (!_column.DataControl.FilterDescriptors.Contains(_compositeFilter)) {
    147.            _column.DataControl.FilterDescriptors.Add(_compositeFilter);
    148.        }
    149. 
    150.        this.IsActive = true;
    151. 
    152.        CategoryChange();
    153.    }
    154.}

     

    I found this thread and tried to figure out what it was doing, but it didn't help.

  2. Mark
    Mark avatar
    3 posts
    Member since:
    Feb 2016

    Posted 02 Feb in reply to Mark Link to this post

    I forgot to mention that the error keeps showing up on line 147, above.

    _column.DataControl.FilterDescriptors.Add(_compositeFilter);

  3. UI for WPF is Visual Studio 2017 Ready
  4. Dilyan Traykov
    Admin
    Dilyan Traykov avatar
    368 posts

    Posted 05 Feb Link to this post

    Hello Mark,

    This exception is normally thrown when an element which is not an IFIlterDescriptor is added to the FilterDescriptors collection. But I'm afraid I'm unable to reproduce it on my end.
    Can you please have a look at the Custom Filtering Controls article of the RadGridView documentation, as the recommended approach for creating a Custom FilteringControl is demonstrated in it?
    Please also observe the Custom Filter Control demo from our SDK examples browser.

    Regards,
    Dilyan Traykov
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Back to Top