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