How to toggle grouping via MVVM binding?

11 posts, 1 answers
  1. Brian Sayatovic
    Brian Sayatovic avatar
    91 posts
    Member since:
    Nov 2009

    Posted 22 Dec 2011 Link to this post

    I have a RadGridView whose contents can be usefully viewed grouped or ungrouped, based on a user's whim.  I'd like to expose a Checkbox for the user to toggle the grouping behavior.  Since GroupDescriptors can be added/removed from the RadGridView on-the-fly, this shouldn't be a problem.

    However, I'm using MVVM where I expect aspects like this to be bindable from the ViewModel.  Has anyone managed to accomplish what I seek using pure MVVM binding?
  2. Brian Sayatovic
    Brian Sayatovic avatar
    91 posts
    Member since:
    Nov 2009

    Posted 23 Dec 2011 Link to this post

    I think I just just binding to an ObservableCollection of GroupDescriptors that I either replace, or add/remove from, on the fly:

    <RadGridView ... GroupDescriptors="{Binding MyDescriptors}" ... />
  3. DevCraft banner
  4. Brian Sayatovic
    Brian Sayatovic avatar
    91 posts
    Member since:
    Nov 2009

    Posted 23 Dec 2011 Link to this post

    Oh no!

    System.NotSupportedException: Cannot set read-only property 'Telerik.Windows.Controls.GridView.GridViewDataControl.GroupDescriptors'.

    Say it ain't so! 
  5. Brian Sayatovic
    Brian Sayatovic avatar
    91 posts
    Member since:
    Nov 2009

    Posted 23 Dec 2011 Link to this post

    In lieu of this, I'm making a custom attached dependency property that allows be to binding to a single IGroupDescriptor from my view model and have it added/cleared from the RadGridView's GroupDescriptors, e.g.:

    <RadGridView ... local:Grouping.DynamicGroupDescriptor="{Binding MyGroupDescriptor}" ... />
  6. Answer
    Pavel Pavlov
    Admin
    Pavel Pavlov avatar
    2039 posts

    Posted 27 Dec 2011 Link to this post

    Hello Brian,

    Indeed the GroupDescriptors collection is readonly as we do follow some MS standards and typically will not expose a read write collection typed property.

    An MVVM friendly way of controlling the grouping would be to use a CollectionViewSource.View as ItemsSource for the RadGridView. RadGridView will respect the grouping state of the CollectionViewSource.

    Kind regards,
    Pavel Pavlov
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

  7. Brian Sayatovic
    Brian Sayatovic avatar
    91 posts
    Member since:
    Nov 2009

    Posted 28 Dec 2011 Link to this post

    Had I realized that was a possibility, I would've used it first; however, I've already arrived at my solution so I'll stick with it for now.
  8. Arnaud
    Arnaud avatar
    17 posts
    Member since:
    Mar 2013

    Posted 06 Jul 2015 in reply to Pavel Pavlov Link to this post

    Hello, 

    The grouping with ICollectionView as the ItemsSource is fine. But I did not manage to use an alternate display name (the actual column's header or another string property that is not the group Member). With GroupDescriptors I could use the DisplayContent, but as you said the GroupDescriptors can't be bound to the ViewModel...

    Any idea ?

     

    Arnaud.

  9. Arnaud
    Arnaud avatar
    17 posts
    Member since:
    Mar 2013

    Posted 06 Jul 2015 in reply to Arnaud Link to this post

    ... and what about the ShowColumnWhenGrouped option ?

    Please provide a way to group from ViewModel with respect to the columns definition : Display Header instead of DataMember, Hide column if  ShowColumnWhenGrouped is False, etc... Just as if the column was manually group !

    Arnaud.

  10. Maya
    Admin
    Maya avatar
    4062 posts

    Posted 09 Jul 2015 Link to this post

    Hi Arnaud,

    You can try working with QueryableCollectionView and create GroupDescriptor instead. So, you can have the following source collection property in your ViewModel:
    private QueryableCollectionView clubs;
          public QueryableCollectionView Clubs
          {
              get
              {
                  if (this.clubs == null)
                  {
                      this.clubs = new QueryableCollectionView(Club.GetClubs());
     
                      this.clubs.GroupDescriptors.Add(new GroupDescriptor() { Member = "Name", DisplayContent = "CustomName" });
                  }
     
                  return this.clubs;
              }
          }



    Regards,
    Maya
    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
  11. Arnaud
    Arnaud avatar
    17 posts
    Member since:
    Mar 2013

    Posted 03 Nov 2015 in reply to Maya Link to this post

    Hello Maya,

    In contrary to ICollectionView the QueryableCollectionView does not support the Filter predicate. The code compiles but the Filter Property raises a runtimeError. Why do you leave it if its not supported ? Could you please help me to filter the QueryableCollectionView ?

    Regards, 

    Arnaud.

     

  12. Maya
    Admin
    Maya avatar
    4062 posts

    Posted 03 Nov 2015 Link to this post

    Hi Brian,

    You can filter the collection quite easily with the help of FilterDescriptors collection. For example:
    private QueryableCollectionView clubs;
            public QueryableCollectionView Clubs
            {
                get
                {
                    if (this.clubs == null)
                    {
                        this.clubs = new QueryableCollectionView(Club.GetClubs());
                         
                        this.clubs.GroupDescriptors.Add(new GroupDescriptor() { Member = "Name", DisplayContent = "CustomName" });
                        this.clubs.FilterDescriptors.Add(new FilterDescriptor() { Member = "Name", Value = "Arsenal", Operator = FilterOperator.IsEqualTo });
                    }
     
                    return this.clubs;
                }
            }

    In case you want to work with Filter property, you can create your own custom QCV and override it. 


    Regards,
    Maya
    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
DevCraft banner