How to close RadMenuItem programmatically from cs file.

4 posts, 0 answers
  1. mohan choudhary
    mohan choudhary avatar
    2 posts
    Member since:
    Dec 2009

    Posted 21 Dec 2009 Link to this post

    <telerikNavigation:RadMenu ClickToOpen="True" VerticalAlignment="Top" BorderThickness="0" Background="Transparent" telerik:StyleManager.Theme="{StaticResource Theme}" > 
                                    <telerikNavigation:RadMenuItem Click="RadMenuItem_Click" VerticalAlignment="Top" BorderThickness="0" Background="Transparent" StaysOpenOnClick="False" > 
                                        <telerikNavigation:RadMenuItem.Icon  > 
                                            <Image Source="../../Images/downArrowGray.png"/> 
                                        </telerikNavigation:RadMenuItem.Icon> 
                                        <telerikNavigation:RadMenuItem Name="rmiEdit" Header="Edit" Click="EditItem_Click" BorderThickness="0"></telerikNavigation:RadMenuItem> 
                                        <telerikNavigation:RadMenuItem  Name="rmiDelete"  Header="Delete" Command="{Binding Path=ParentCalendarViewModel.DeleteCalendarCommand}" Click="DeleteItem_Click" BorderThickness="0"></telerikNavigation:RadMenuItem> 
                                        <telerikNavigation:RadMenuItem  Name="rmiInactivate"  Header="Inactivate" BorderThickness="0" ></telerikNavigation:RadMenuItem> 
                                        <telerikNavigation:RadMenuItem  Name="rmiAddCalendarDate" BorderThickness="0" Command="{Binding Path=ParentCalendarViewModel.AddNewCalendarDateRow}" Header="Add a Calendar Date" ></telerikNavigation:RadMenuItem> 
                                        <telerikNavigation:RadMenuItem  Name="rmiAddNote" BorderThickness="0" Header="Add a Note" ></telerikNavigation:RadMenuItem> 
                                        <telerikNavigation:RadMenuItem  Name="rmiViewHistory" BorderThickness="0" Header="View History" ></telerikNavigation:RadMenuItem> 
                                    </telerikNavigation:RadMenuItem> 
     
                                </telerikNavigation:RadMenu> 

    The above is my RadMenuItem, how can I close the submenu programmatically from cs code file.
    when the user clicks on the RadMenuItem , the submenu opens, but on some conditions how can I close it from cs file code.  and I have access to the RadMenuItem object.

    The following is the code attached with the scenario -

    <UserControl x:Class="ContextMenu_ShowFromCode.Page" 
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
                 xmlns:core="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls" 
                 xmlns:telerikNavigation="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.Navigation"   
                 xmlns:nav="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.Navigation"
        <Grid x:Name="LayoutRoot" Background="White"
            <StackPanel Orientation="Vertical"
                
     
                <telerikNavigation:RadMenu ClickToOpen="True" VerticalAlignment="Top" BorderThickness="0" Background="Transparent" > 
                    <telerikNavigation:RadMenuItem Click="RadMenuItem_Click" VerticalAlignment="Top" BorderThickness="0" Background="Transparent"  > 
                        <telerikNavigation:RadMenuItem.Icon  > 
                            <Image Source="../../Images/downArrowGray.png"/> 
                        </telerikNavigation:RadMenuItem.Icon> 
                        <telerikNavigation:RadMenuItem Name="rmiEdit" Header="Edit" Click="EditItem_Click" BorderThickness="0"></telerikNavigation:RadMenuItem> 
                        <telerikNavigation:RadMenuItem  Name="rmiDelete"  Header="Delete" BorderThickness="0"></telerikNavigation:RadMenuItem> 
                        <telerikNavigation:RadMenuItem  Name="rmiInactivate"  Header="Inactivate" BorderThickness="0" ></telerikNavigation:RadMenuItem> 
                        <telerikNavigation:RadMenuItem  Name="rmiAddCalendarDate" BorderThickness="0" Header="Add a Calendar Date" ></telerikNavigation:RadMenuItem> 
                        <telerikNavigation:RadMenuItem  Name="rmiAddNote" BorderThickness="0" Header="Add a Note" ></telerikNavigation:RadMenuItem> 
                        <telerikNavigation:RadMenuItem  Name="rmiViewHistory" BorderThickness="0" Header="View History" ></telerikNavigation:RadMenuItem> 
                    </telerikNavigation:RadMenuItem> 
     
                </telerikNavigation:RadMenu> 
                 
                <TextBox Name="txtbxTemp" LostFocus="TextBox_LostFocus" ></TextBox
            </StackPanel> 
        </Grid> 
    </UserControl> 
     

    using System; 
    using System.Collections.Generic; 
    using System.Linq; 
    using System.Net; 
    using System.Windows; 
    using System.Windows.Controls; 
    using System.Windows.Documents; 
    using System.Windows.Input; 
    using System.Windows.Media; 
    using System.Windows.Media.Animation; 
    using System.Windows.Shapes; 
    using Telerik.Windows.Controls; 
     
    namespace ContextMenu_ShowFromCode 
        public partial class Page : UserControl 
        { 
            public Page() 
            { 
                InitializeComponent(); 
            } 
     
          
            private void RadMenuItem_Click(object sender, RoutedEventArgs e) 
            { 
                 
            } 
     
            private void TextBox_LostFocus(object sender, RoutedEventArgs e) 
            { 
                // I want to hide the already opened submenu here. 
                // And I have the RadMenuItem object available. 
            } 
        } 
     

    In the above text box the user enters some value and hits the RadMenuItem, the TextBox_LostFocus is fired and the submenu is also opened.  But I don't want the RadMenuItem's submenu to open up.






  2. Hristo
    Admin
    Hristo avatar
    832 posts

    Posted 23 Dec 2009 Link to this post

    Hi mohan choudhary,

    Are you focusing the textbox from code on MenuItem click?
    Open/Close of RadMenu from code is not supported yet. But if you open then menu you cannot click another control on the page unless menu is closed.

    Regards,
    Hristo
    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. John Thompson
    John Thompson avatar
    44 posts
    Member since:
    Dec 2009

    Posted 30 Jan 2010 Link to this post

    The only solution I have been able to work out is using ItemSource binding to create the RadMenuItems and the setting the ItemsSource to null when in the ItemClick event.
  5. troy
    troy avatar
    20 posts
    Member since:
    Jun 2008

    Posted 26 Mar 2010 Link to this post

    if you have the RadMenuItem, you can close it like this:

     

     

    RadMenuItemAutomationPeer mip = new RadMenuItemAutomationPeer(menuItemRoot);

     

    mip.Invoke();

    where menuItemRoot is your menu item.  The Invoke() basically calls the default action which is click, so if the menu is open, it will close it.

    You can also use this same method to open a menu, but to do this, you have to start at the root and work your way to your menu item.

Back to Top