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

How to do sorting on MVVM View model

1 Answer 518 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Dharmesh
Top achievements
Rank 1
Dharmesh asked on 25 May 2011, 10:44 AM
Hi !
We are using RADGrid for displaying a set of products. and We are using WPF and MVVM as framework.

I have following scenario :

I need to sort a grid on two columns - for example Product type and Product Name.
Consider following as a collection

ProductType         ProductName
1                            Product 1
1                            Product 2
3                            Product 5
2                            Product 3

So we need to do sort first on Product Type and secondly on name.

I have did following:
Following is my xaml :

<

 

 

telerik:RadGridView x:Name="gvProductData"

 

 

 

 

Grid.Row="1"

 

 

 

 

ShowGroupPanel="False"

 

 

 

 

IsFilteringAllowed="False"

 

 

 

ScrollViewer.VerticalScrollBarVisibility="Auto"

 

 

 

ScrollViewer.HorizontalScrollBarVisibility="Auto"

 

 

 

 

ItemsSource="{Binding FilteredProducts, Mode=TwoWay}"

 

 

 

 

IsReadOnly="True" AutoGenerateColumns="False"

 

 

 

BorderThickness="0"

 

 

 

HorizontalAlignment="Left"

 

 

 

SelectedItem="{Binding SelectedProduct, Mode=TwoWay}"

 

 

 

Visibility="{Binding GridViewVisibility}"

 

 

 

>

 

 

 

 

<telerik:RadGridView.Columns>

 

 

 

 

<telerik:GridViewDataColumn Header="Quick Code" DataMemberBinding="{Binding ProductType}" Width="100" TextWrapping="Wrap" />

 

 

 

 

 

 

 

 

<telerik:GridViewDataColumn Header="Description" DataMemberBinding="{Binding ProductName}" Width="*" TextWrapping="Wrap"/>
</telerik:RadGridView.Columns>

 

 

 

 

 

<i:Interaction.Triggers>

 

 

 

 

<i:EventTrigger EventName="Sorting">

 

 

 

 

<cmd:EventToCommand Command="{Binding Path=OnGridSortCommand}" PassEventArgsToCommand="True">

 

 

 

 

</cmd:EventToCommand>

 

 

 

 

</i:EventTrigger>

 

 

 

 

</i:Interaction.Triggers>

 

 

 

 

</telerik:RadGridView >


Following is my View Model :

 

 

private

 

 

void OnGridSortClicked(RoutedEventArgs e)

 

 

{

 

 

 

 

IEnumerable<CustomModelProductSelection> qry = from p in this.Products

 

 

 

 

 

orderby p.FavoriteAsEnum, p.ProductDescription

 

 

 

 

 

select p;

 

 

FilteredProducts =

 

 

new List<CustomModelProductSelection>(qry);

 

 

}

 

 

 

 

So here in my view model I can get the column header on which user clicked for sorting from RoutedEventArgs parameter. But ideally i shall not be doing it as it violates the MVVM pattern (ViewModel should not have tightly coupled with view)

Do you know any other way to get the sort column name from grid? like maintaining the header collection and binding to grid..

Thanks!
Dharmesh

1 Answer, 1 is accepted

Sort by
0
Accepted
Ivan Ivanov
Telerik team
answered on 25 May 2011, 11:42 AM
Hi Dharmesh,

When sorting is performed through the UI, a ColumnSortDescriptor is added to RadGridView's SortDescriptors collection. Unfortunately you cannot bind your ViewModel directly to it, since it is a read-only dependency property.  

Best wishes,
Ivan Ivanov
the Telerik team
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 Public Issue Tracking system and vote to affect the priority of the items
Tags
GridView
Asked by
Dharmesh
Top achievements
Rank 1
Answers by
Ivan Ivanov
Telerik team
Share this question
or