Commanding in MVVM : Example

4 posts, 0 answers
  1. Robert Pelzer
    Robert Pelzer avatar
    15 posts
    Member since:
    Aug 2009

    Posted 16 Nov 2009 Link to this post

    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!
  2. Ivan
    Admin
    Ivan avatar
    519 posts

    Posted 22 Nov 2009 Link to this post

    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.
  3. DevCraft banner
  4. James
    James avatar
    2 posts
    Member since:
    Sep 2008

    Posted 21 Jan 2010 Link to this post

    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!!!!
  5. Ivan
    Admin
    Ivan avatar
    519 posts

    Posted 26 Jan 2010 Link to this post

    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.
Back to Top