Menus and MVVM

12 posts, 0 answers
  1. Corrado Cavalli
    Corrado Cavalli avatar
    6 posts
    Member since:
    Feb 2007

    Posted 19 Aug 2009 Link to this post

    Any chance to have a Command property on RadMenuItem? that would help M-V-VM development a lot.

    Thanks
    Corrado
  2. Valeri Hristov
    Admin
    Valeri Hristov avatar
    2252 posts

    Posted 21 Aug 2009 Link to this post

    Hello Corrado,

    There is no ICommandSource interface in Silverlight, yet, hence RadMenuItem does not have a Command/CommandParameter/CommandTarget properties.

    On the other hand, we provide our own implementation for routed commands, that include the interface, but it was first intended only for internal use and that's why most of the controls do not implement it. I am currently updating RadMenuItem and most probably it will get the Command property with the internal build next week.

    Greetings,
    Valeri Hristov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  3. DevCraft banner
  4. Neal Sanche
    Neal Sanche avatar
    6 posts
    Member since:
    Oct 2009

    Posted 30 Oct 2009 Link to this post

    Any updates on this since? I would also love to be able to bind ICommand to a property on the RadContextMenu. In the meantime I am trying to wire up an attached behavior, but I'm not quite sure how that's going to work yet.

    -Neal
  5. Valeri Hristov
    Admin
    Valeri Hristov avatar
    2252 posts

    Posted 02 Nov 2009 Link to this post

    Hi Neal,

    RadMenu/RadContextMenu for Silverlight and WPF will provide ICommandSource implementation as of Q3 2009 which will be released this week.

    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.
  6. Neal Sanche
    Neal Sanche avatar
    6 posts
    Member since:
    Oct 2009

    Posted 02 Nov 2009 Link to this post

    Thanks! That's great, I can hardly wait.

    -Neal
  7. Neal Sanche
    Neal Sanche avatar
    6 posts
    Member since:
    Oct 2009

    Posted 04 Nov 2009 Link to this post

    Just a follow-up. I have upgraded to the Q3 release and have tried doing a binding with a RadMenuItem using its Command property to a property on my ViewModel class (of type ICommand) and it works! This is a great new feature of the Q3 release, and isn't mentioned very much in the release notes (on my initial skim of them).

    Here's my XAML:

    <Grid Height="39" MinWidth="90"
       <Controls:RadContextMenu.ContextMenu> 
          <Controls:RadContextMenu> 
             <Controls:RadMenuItem Command="{Binding DeleteQueryCommand}" Header="Delete Query"/> 
          </Controls:RadContextMenu> 
       </Controls:RadContextMenu.ContextMenu> 
       <TextBlock Text="Query 1"/> 
    </Grid> 

    I've been following a DelegateCommand pattern in my view model classes, and the code for this command looks something like the following:

            #region DeleteQuery Command 
     
            private DelegateCommand m_deleteQueryCommand; 
     
            public ICommand DeleteQueryCommand 
            { 
                get 
                { 
                    if (m_deleteQueryCommand == null) m_deleteQueryCommand = new DelegateCommand(DeleteQuery, DeleteQueryCanExecute); 
                    return m_deleteQueryCommand; 
                } 
            } 
     
            private void DeleteQueryCanExecuteChanged() 
            { 
                m_deleteQueryCommand.RaiseCanExecuteChanged(); 
            } 
     
            private bool DeleteQueryCanExecute() 
            { 
                return true
            } 
     
            private void DeleteQuery() 
            { 
                QueryDeleted.Fire(this, EventArgs.Empty); 
            } 
            #endregion 

    I've even tested that the menu item is grayed out when the DeleteQueryCanExecute method returns false!

    Thanks for the great work!

    -Neal
  8. Kevin Finke
    Kevin Finke avatar
    7 posts
    Member since:
    Jan 2006

    Posted 13 Nov 2009 Link to this post

    This is exactly what I want!

    But where oh where is the rest of the command framework?  Where is DelegateCommand defined?  I seem to be missing something!
  9. Valeri Hristov
    Admin
    Valeri Hristov avatar
    2252 posts

    Posted 17 Nov 2009 Link to this post

    Hi Kevin,

    Our command framework was designed to be used the same way as the standard WPF commands, hence there is no delegate commands, etc. It was first developed to provide a compatibility layer for the RadScheduler port to Silverlight, later we began using it in other controls. The ICommandSource implementation in RadMenuItem is standard and will work with any command framework, such as Prism for example.

    Sincerely yours,
    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.
  10. bohebolo
    bohebolo avatar
    31 posts
    Member since:
    Aug 2010

    Posted 07 Sep 2010 Link to this post

    Is it possible to use command with dynamic data binding on RadMenu?
    And if yes, how to do that?
  11. bohebolo
    bohebolo avatar
    31 posts
    Member since:
    Aug 2010

    Posted 07 Sep 2010 Link to this post

    I've got the answer now, and yes it can actually.
    Just bind Command & CommandParameter properties in ContainerBindingCollection.
    EDIT: layoutRoot is a grid in my view which it's datacontext is bound to my viewmodel.

    <telerik:HierarchicalDataTemplate x:Key="mainMenuTemplate" ItemsSource="{Binding SubItems}">
        <telerik:ContainerBinding.ContainerBindings>
            <telerik:ContainerBindingCollection>
                <telerik:ContainerBinding PropertyName="Icon" Binding="{Binding Icon}" />
                <telerik:ContainerBinding PropertyName="Command" Binding="{Binding
                          DataContext.MenuOpenCommand, ElementName=layoutRoot, Mode=OneWay}"
    />
                <telerik:ContainerBinding PropertyName="CommandParameter" Binding="{Binding}" />
            </telerik:ContainerBindingCollection>
        </telerik:ContainerBinding.ContainerBindings>
        <TextBlock Text="{Binding Text}" />
    </telerik:HierarchicalDataTemplate>
  12. Kaloyan
    Admin
    Kaloyan avatar
    920 posts

    Posted 07 Sep 2010 Link to this post

    Hello bohe,

    The approach taken is the correct one. Let us know if you are facing any further issues.

    Kind regards,
    Kaloyan
    the Telerik team
    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 Public Issue Tracking system and vote to affect the priority of the items
  13. Hicham
    Hicham avatar
    3 posts
    Member since:
    Jul 2012

    Posted 23 Mar 2012 Link to this post

    Hi,
    I wanna send a commandParameter to a command, but i get always null for the parameter:

     <telerik:RadContextMenu.ContextMenu>
                                    <telerik:RadContextMenu>
                                        <telerik:RadContextMenu.Items>
                                            <telerik:RadMenuItem Header="Fonctions Mathématiques"  >
                                                <telerik:RadMenuItem Header="INT()" Command="{Binding TestCommand}" CommandParameter="{Binding ElementName=txtFormula}"/>
                                                <telerik:RadMenuItem Header="AVERAGE()" />
                                                <telerik:RadMenuItem Header="ROUND()" />
                                                <telerik:RadMenuItem Header="ROUNDUP()" />
                                                <telerik:RadMenuItem Header="ROUNDDOWN()" />
                                            </telerik:RadMenuItem>
                                            <telerik:RadMenuItem Header="Operateurs" >
                                                <telerik:RadMenuItem Header="&lt;" />
                                                <telerik:RadMenuItem Header="&gt;" />
                                                <telerik:RadMenuItem Header="&lt;&gt;" />
                                            </telerik:RadMenuItem>
                                        </telerik:RadContextMenu.Items>
                                    </telerik:RadContextMenu>
                                </telerik:RadContextMenu.ContextMenu>


    the command :

     string testCommand = "";
            public ICommand TestCommand
            {
                get
                {
                    return new RelayCommand<System.Windows.Controls.RichTextBox>((o) =>
                    {
                        if (o != null)
                            testCommand = o.ExtentWidth.ToString();
     
     
                        
                    });
                }
            }


    do you have any idea about this issue?

    thanks.
Back to Top
DevCraft banner