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

CommandManager for general use

6 Answers 278 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Clint Singer
Top achievements
Rank 1
Clint Singer asked on 30 Oct 2009, 06:05 PM
Hi,

I have been trying to find information about the CommandManager that I found in the silverlight controls library.  So far the examples that I have found have been specific to the scheduler and dock controls.

Is it possible to use the command manager in other uses for an MVVM pattern?  I assume it is there to fill in the gap between WPF and Silverlight?  There hasn't been any helpful documentation that I could use to figure out how to use it correctly in this scenario.

If it is possible, an example would be great!

Cheers,
Clint

6 Answers, 1 is accepted

Sort by
0
Valeri Hristov
Telerik team
answered on 02 Nov 2009, 08:32 AM
Hello Clint,

The CommandManager and our RoutedCommand implementation for Silverlight was originally intended for internal use as a compatibility layer for the development of RadScheduler and RadDocking, hence it is not documented and advertized. This is slowly changing now, since we are providing more controls that use commands - Buttons, Menu, ContextMenu, etc. Here is a simple example, demonstrating the basic use of our command framework:
http://demos.telerik.com/silverlight/beta/#Buttons/Commands

Since it was intended to be as compatible as possible to the WPF command framework, its usage is virtually the same, with only a few differences:
- the command instances (the static class MyCommands from the example above) must be initialized before the XAML parser is executed. This is because the parser has some limitations and cannot correctly parse the XAML representation of the commands. In two words, you should "touch" a static property of the commands class before the InitializeComponent() call.

- when a command is created, you should set its Name to be the same as the name that will be used in XAML. Again, this is because of the limitations of the Silverlight parser. In other words, if you want to write the following XAML:
<RadButton ... Command="local:MyCommands.Copy" ... />
you should have a static class named MyCommands, that registered a command, named Copy:
public static class MyCommands
{
 public static readonly RoutedCommand Copy =
  new RoutedUICommand("Copy", typeof(MyCommands));
...
}
The name of the property, holding the command instance actually does not matter, but I would recommend naming it after the command because the syntax implies that they are the same.

- you add command bindings using the attached property CommandManager.CommandBindings. Instead, you could directly add event handlers to CanExecute and Executed as we do in the example above.
- you add input bindings using the attached property CommandManager.InputBindings.

Please, let us know if you need additional information.

Best wishes,
Valeri Hristov
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
Clint Singer
Top achievements
Rank 1
answered on 02 Nov 2009, 06:10 PM
Hi,

Thanks for the information.

I managed to get it to work with the RadButton as per the example in the demos.  I will now try to use it with other controls that may not know anything about your CommandManager. 

I do have a question about how to route the OnExecute and OnCanExecute to my viewmodel instead of the code behind file.

Any ideas?

Cheers,
Clint
0
Clint Singer
Top achievements
Rank 1
answered on 03 Nov 2009, 12:55 AM
Could you also give an example of how to use it with the RadMenu if that is possible?

I am currently building a menu of sorts using RadDropDownButtons and it would be nicer to just use the menu control.

Thanks.

Cheers,
Clint
0
Valeri Hristov
Telerik team
answered on 03 Nov 2009, 01:10 PM
Hi Clint,

Here is a simple application that demonstrates the ICommandSource implementation of RadMenu and RadContextMenu. Note that to run it you will need the Telerik assemblies that will be released later today.

Regarding the handling the OnExecuted and OnCanExecute in the ViewModel - using the CommandBindings you could attach handlers that are declared in a separate class, thus moving most of the logic outside the View. Something like this:

public Page()
{
    InitializeComponent();
    var model = this.Resources["ViewModel"] as MyViewModel;
    CommandManager.SetCommandBindings(this, new CommandBindingCollection()
    {
        new CommandBinding(MenuCommands.New, model.Command_Executed, model.New_CanExecute),
        new CommandBinding(MenuCommands.Open, model.Command_Executed, model.Open_CanExecute),
        new CommandBinding(MenuCommands.Save, model.Command_Executed, model.Save_CanExecute)
    });
}

Greetings,

Valeri Hristov
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
Clint Singer
Top achievements
Rank 1
answered on 03 Nov 2009, 03:59 PM
Hi,

Thank you for your response. I look forward to the next release. 

Do you know if there is a way to do the binding to the viewmodel from within the xaml page itself?  Or is it necessary to do it via the code behind file.  I am not a MVVM purists so I am not opposed to doing some code in the code behind, but it would be nice to know how to do it in the XAML too.

Cheers,
Clint
0
Valeri Hristov
Telerik team
answered on 05 Nov 2009, 03:46 PM
Hello Clint,

You could check my blog post about RadCoverFlow and Routed Commands for a sample application that demonstrates how to handle the Commands' Executed and CanExecute events in the ViewModel:
http://blogs.telerik.com/valerihristov/posts/09-11-05/data-binding_telerik_coverflow_for_silverlight_some_routed_commands_goodness.aspx

I hope it would be of help.

Kind regards,
Valeri Hristov
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
General Discussions
Asked by
Clint Singer
Top achievements
Rank 1
Answers by
Valeri Hristov
Telerik team
Clint Singer
Top achievements
Rank 1
Share this question
or