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!
Thanks!
3 Answers, 1 is accepted
0
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.
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:
where "vws" references the namespace where MyViewModel is contained.
In the MyViewModel class, the commands are defined as:
I follow the example project and have the following code in the XAML code-behind:
The GetCommandBindings method is implemented as:
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!!!!
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}}" /> |
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
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.
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.