Hi there
Is there an MVVM-pattern solution to trigger a ViewModel refresh (from an rpc-database backend) when the groupdescriptors of a DataGird are changed?
Right now I've solved this issue with a C# event as below, but I'd like to have either the groupdescriptors bound to the viewmodel or I'd like to use an ICommand following MVVM principles.
Current solution:
dataGrid.GroupDescriptors.CollectionChanged += GroupDescriptors_CollectionChanged;
private
void
GroupDescriptors_CollectionChanged(
object
sender, NotifyCollectionChangedEventArgs e)
{
string
groups =
""
;
foreach
(Telerik.Data.Core.PropertyGroupDescriptor gridGroup
in
dataGrid.GroupDescriptors)
{
groups += (String.IsNullOrWhiteSpace(groups) ?
""
:
","
) +
((gridGroup.SortOrder == Telerik.Data.Core.SortOrder.Ascending) ?
"+"
:
"-"
) +
gridGroup.PropertyName;
}
This.ViewModel.GroupStringForRpcCall = groups;
}
Desired solution following MVVM principles:
<gridCommands:DataGridUserCommand Id=
"ColumnHeaderTap"
EnableDefaultCommand=
"True"
Command=
"{Binding TelerikColumnHeaderSortCommand, Source={StaticResource ViewModel}}"
/>
internal
class
TelerikRpcSortCommand : ICommand
{
private
IRpcViewModel ViewModel;
public
TelerikRpcSortCommand(IRpcViewModel viewModel)
{
ViewModel = viewModel;
}
public
event
EventHandler CanExecuteChanged;
public
bool
CanExecute(
object
parameter)
{
var context = parameter
as
ColumnHeaderTapContext;
return
true;
}
public
async
void
Execute(
object
parameter)
{
var context = parameter
as
ColumnHeaderTapContext;
var column = context.Column
as
DataGridTypedColumn;
string
order =
null
;
if
(column.SortDirection == SortDirection.Ascending)
order =
"+"
+column.PropertyName;
else
if
(column.SortDirection == SortDirection.Descending)
order =
"-"
+ column.PropertyName;
else
order =
""
;
ViewModel.Order = order;
ViewModel.LoadDataAsync();
}
}