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

Commanding in MVVM : Example

3 Answers 233 Views
Buttons
This is a migrated thread and some comments may be shown as answers.
Robert Pelzer
Top achievements
Rank 2
Robert Pelzer asked on 16 Nov 2009, 04:19 PM
I have been looking for documentation or an example of how to use commanding within Silverlight for the button control in a MVVM architecture, but haven't found any.  I've seen the example on how to use commands to bind to a method on the local code-behind, but haven't seen one that uses something like a ViewModel.  Simply put, I need to bind a command to a method on a different object (i.e. the ViewModel).

Thanks!

3 Answers, 1 is accepted

Sort by
0
Ivan
Telerik team
answered on 23 Nov 2009, 03:25 AM
Hi Ryan,

Sorry for the late response.

Please preview the attached example and let us know if you need something else.

Best wishes,
Ivan
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
James
Top achievements
Rank 1
answered on 21 Jan 2010, 02:58 PM
So I've tried to follow the example in the attached solution and have it partially working.  What is frustrating is that I'm positive it all was working at one point but for some reason, with no changes to the affected code, it no longer does.

In my application, I have two RadRibbonButtons defined as:

<telerikRibbon:RadRibbonButton Command="vms.MyViewModel.ShowProperties" 
                               LargeImage="/MyApp;component/Images/32x32/Properties.png" 
                               Size="Large" 
                               Text="{Binding Command.Text, RelativeSource={RelativeSource Self}}" /> 
 
<telerikRibbon:RadRibbonButton Command="vms.MyViewModel.CreateDataSource" 
                               LargeImage="/MyApp;component/Resources/Images/32x32/CreateDataSource.png" 
                               Size="Large" 
                               Text="{Binding Command.Text, RelativeSource={RelativeSource Self}}" /> 
 
where "vws" references the namespace where MyViewModel is contained.

In the MyViewModel class, the commands are defined as:
public static readonly RoutedUICommand CreateDataSource = new RoutedUICommand("New Data Source""CreateDataSource"typeof(MyViewModel));  
 
public static readonly RoutedUICommand ShowProperties = new RoutedUICommand("Properties""ShowProperties"typeof(MyViewModel));  
 

I follow the example project and have the following code in the XAML code-behind:
CommandManager.SetCommandBindings(this, viewModel.GetCommandBindings()); 

The GetCommandBindings method is implemented as:
CommandBindingCollection commands = new CommandBindingCollection()  
{  
    new CommandBinding(CreateDataSource, CreateDataSourceCommand_Executed, CreateDataSourceCommand_CanExecute),  
    new CommandBinding(ShowProperties, ShowPropertiesCommand_Executed, ShowPropertiesCommand_CanExecute)  
}; 

After all of this, the ShowProperties command isn't working - but the other command does!  In the UI, the button shows no text and nothing happens when I click it.  If I change the ShowPropertiesCommand_CanExecute method to return false, the button remains enabled.

What has me totally perplexed is that the other button works perfectly!!!

What am I missing?  Is there some difference between the two that I am not seeing?  HELP!!!!
0
Ivan
Telerik team
answered on 26 Jan 2010, 02:46 PM
Hello James,

This partially working commanding sounds very strange. Could send us your project to investigate the issue.

Looking forward for your reply.

Best wishes,
Ivan
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Tags
Buttons
Asked by
Robert Pelzer
Top achievements
Rank 2
Answers by
Ivan
Telerik team
James
Top achievements
Rank 1
Share this question
or