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

Getting Command to communicate with ViewModel

3 Answers 162 Views
DataGrid
This is a migrated thread and some comments may be shown as answers.
Jeremiah
Top achievements
Rank 1
Jeremiah asked on 08 Oct 2017, 04:09 AM

Okay, so I'm learning MVVM, and I think I understand it, but one aspect of the RadDataGrid has been frustrating me.

I am trying to use CommitEdit in an MVVM like way, and I have tried two things:

<GridCommands:DataGridUserCommand Id="CommitEdit" Command="{Binding CommitChanges}"/>

and

<Budgeteer:BudgetViewCommand/> where "Budgeteer" is the namespace, and "BudgetViewCommand" is a public class derived from DataGridCommand.

Here is the relevant source:

xmlns:Budgeteer="using:Budgeteer"
xmlns:Grid="using:Telerik.UI.Xaml.Controls.Grid"
xmlns:GridCommands="using:Telerik.UI.Xaml.Controls.Grid.Commands"
 
<Grid:RadDataGrid.Commands>
    <!--<GridCommands:DataGridUserCommand Id="CommitEdit" Command="{Binding CommitChanges}"/>-->
    <Budgeteer:BudgetViewCommand/>
</Grid:RadDataGrid.Commands>

 

When I try to use GridCommands, nothing happens. The binding to CommitChanges seems to be correct, and similar bindings work in other controls. But it just won't do anything. There are no errors in my output.

When I try to use BudgetViewCommand, which is inherited from DataGridCommand, both the CanExecute and Execute work, but I haven't a clue how to get it to communicate with my ViewModel. I don't even understand how I managed to get an instance of BudgetViewCommand to begin with! The XAML created it for me?

One of the things that kinda frustrates me as I've been trying to learn MVVM with UWP is how much "under the hood" is going on. I'm trying to catch up on the 2017 way of doing things when my mind is back in the '90s with procedural / OOP way of doing things, and I'm used to explicitly telling the machine exactly what I want it to do. But now I have an instance of a class being made, and since I have no idea how it was created, I also have no way of knowing how it's going to access my ViewModel. Sigh.

3 Answers, 1 is accepted

Sort by
0
Jeremiah
Top achievements
Rank 1
answered on 08 Oct 2017, 04:49 PM

So in the BudgetViewCommand class, in the Execute method, I added:

((BudgetViewModel)(this.Owner.DataContext)).CommitChanges();

 

Which worked.

I got the idea from one of the responses to a question about using ViewModel properties in the code behind on MSDN. I'm not entirely confident it's "correctly MVVM," but it seems to do the job.

This seems like a lot more work than bindings or events, though. I'm not sure why the DataGrid commands require a workaround, while simple buttons allow me to bind directly to the ViewModel.

0
Accepted
Nasko
Telerik team
answered on 11 Oct 2017, 10:40 AM
Hi Jeremiah,

Both approaches with using the custom command and the DataGridUserCommand more or less are MVVM orientated. The solution you have found does not break the MVVM patter and you shouldn't have concerns of using it. However, using the DataGridUserCommand should be working as well. What we believe might have prevent the usage of that approach is that for some reason the binding is not working as expected. Attached you can find a sample that demonstrates how the DataGridUserCommand could be used.

We hope this will help you.

Regards,
Nasko
Progress 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
Jeremiah
Top achievements
Rank 1
answered on 12 Oct 2017, 02:32 AM
Thank you for your reply, and for the sample code. I can already see differences that highlight things I may have missed.
Tags
DataGrid
Asked by
Jeremiah
Top achievements
Rank 1
Answers by
Jeremiah
Top achievements
Rank 1
Nasko
Telerik team
Share this question
or