This is a migrated thread and some comments may be shown as answers.

How to toggle grouping via MVVM binding?

10 Answers 380 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Brian Sayatovic
Top achievements
Rank 1
Brian Sayatovic asked on 22 Dec 2011, 10:56 PM
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?

10 Answers, 1 is accepted

Sort by
0
Brian Sayatovic
Top achievements
Rank 1
answered on 23 Dec 2011, 04:20 PM
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}" ... />
0
Brian Sayatovic
Top achievements
Rank 1
answered on 23 Dec 2011, 04:52 PM
Oh no!

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

Say it ain't so! 
0
Brian Sayatovic
Top achievements
Rank 1
answered on 23 Dec 2011, 05:26 PM
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}" ... />
0
Accepted
Pavel Pavlov
Telerik team
answered on 27 Dec 2011, 12:59 PM
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 >>

0
Brian Sayatovic
Top achievements
Rank 1
answered on 28 Dec 2011, 05:42 PM
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.
0
Arnaud
Top achievements
Rank 1
answered on 06 Jul 2015, 01:05 PM

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.

0
Arnaud
Top achievements
Rank 1
answered on 06 Jul 2015, 03:44 PM

... 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.

0
Maya
Telerik team
answered on 09 Jul 2015, 12:04 PM
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
0
Arnaud
Top achievements
Rank 1
answered on 03 Nov 2015, 03:26 PM

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.

 

0
Maya
Telerik team
answered on 03 Nov 2015, 03:42 PM
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
Tags
GridView
Asked by
Brian Sayatovic
Top achievements
Rank 1
Answers by
Brian Sayatovic
Top achievements
Rank 1
Pavel Pavlov
Telerik team
Arnaud
Top achievements
Rank 1
Maya
Telerik team
Share this question
or