Would like to open menu after async menu items arrive...

2 posts, 0 answers
  1. Rob
    Rob avatar
    238 posts
    Member since:
    Jan 2009

    Posted 24 Feb 2011 Link to this post

    Hello,

    I have a RadMenuItem that creates it's subitems asynchronously after the user hovers the menu (the first time only)...  Unfortunately, after they hover the menu item and then the subitems are created, I need to "jiggle" the mouse to get the menu to actually open.  Is there a way to force the submenu to open after the subitems are created?

    Here's my sample code...

    A custom control defined like this:
    <Style TargetType="local:SomeCustomControl">
       <Setter Property="Template">
          <Setter.Value>
             <ControlTemplate TargetType="local:SomeCustomControl">
                <telerik:RadMenu ShowDelay="0" HideDelay="0">
                   <telerik:RadMenuItem x:Name="rmi" Header="^" ItemsSource="{TemplateBinding MyList}">
                   </telerik:RadMenuItem>
                </telerik:RadMenu>
             </ControlTemplate>
          </Setter.Value>
       </Setter>
    </Style>

    using System;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Ink;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;
    using Telerik.Windows.Controls;
    using System.ComponentModel;
    using System.Threading;
    using System.Collections.ObjectModel;
      
      
    namespace SilverlightApplication36
    {
      
      
        public class SomeCustomControl : Control
        {
              
            private bool _eventsRegistered = false;
              
            public SomeCustomControl()
            {
                DefaultStyleKey = typeof(SomeCustomControl);
            }
      
            private RadMenuItem _rootMenuItem = null;
      
            public override void OnApplyTemplate()
            {
                base.OnApplyTemplate();
      
                if (_rootMenuItem == null)
                {
                    _rootMenuItem = GetTemplateChild("rmi") as RadMenuItem;
                    if (!_eventsRegistered && _rootMenuItem != null)
                    {
                        _eventsRegistered = true;
                        _rootMenuItem.MouseEnter += _rootMenuItem_MouseEnter;
                    }
                }
      
            }
      
            void _rootMenuItem_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
            {
                if (_rootMenuItem.Items.Count == 0)
                {
                    _rootMenuItem.MouseEnter -= _rootMenuItem_MouseEnter;
                      
                    //Simulate the delay to retrieve menu items using a BackgroundWorker()...
                    BackgroundWorker w = new BackgroundWorker();
                    w.DoWork += new DoWorkEventHandler(w_DoWork);
                    w.RunWorkerCompleted += new RunWorkerCompletedEventHandler(w_RunWorkerCompleted);
                    w.RunWorkerAsync();
                }
            }
      
            void w_DoWork(object sender, DoWorkEventArgs e)
            {
                Thread.Sleep(3000);
            }
      
            void w_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
            {
                SetValue
                (
                    SomeCustomControl.MyListProperty,
                    new ObservableCollection<RadMenuItem> 
                        {
                            new RadMenuItem() { Header = "aaa", Icon = new Border() { BorderThickness = new Thickness(1), Background = new SolidColorBrush(Colors.Purple), BorderBrush = new SolidColorBrush(Colors.Orange), Height = 16, Width = 16 } },
                            new RadMenuItem() { Header = "bbb", Icon = new Border() { BorderThickness = new Thickness(1), Background = new SolidColorBrush(Colors.Purple), BorderBrush = new SolidColorBrush(Colors.Orange), Height = 16, Width = 16 } },
                            new RadMenuItem() { Header = "ccc", Icon = new Border() { BorderThickness = new Thickness(1), Background = new SolidColorBrush(Colors.Purple), BorderBrush = new SolidColorBrush(Colors.Orange), Height = 16, Width = 16 } },
                            new RadMenuItem() { Header = "ddd", Icon = new Border() { BorderThickness = new Thickness(1), Background = new SolidColorBrush(Colors.Purple), BorderBrush = new SolidColorBrush(Colors.Orange), Height = 16, Width = 16 } }
                        }
                );
                  
                //Would like to "open" the menu here now that it has values!!!
                //_rootMenuItem.OpenMePlease!
            }
              
            public static readonly DependencyProperty MyListProperty = DependencyProperty.Register("MyList", typeof(ObservableCollection<Telerik.Windows.Controls.RadMenuItem>), typeof(SomeCustomControl), null);
      
            public ObservableCollection<RadMenuItem> MyList
            {
                get
                {
                    return (ObservableCollection<RadMenuItem>)GetValue(MyListProperty);
                }
                set
                {
                    SetValue(MyListProperty, value);
                }
            }
      
              
      
        }
      
      
    }


    Notice that when you hover the top level menu item, the worker is executed (to simulate an async retrieval of menu items) at that point you need to move the mouse (even a single pixel) to get the menu to actually open...


  2. Hristo
    Admin
    Hristo avatar
    832 posts

    Posted 25 Feb 2011 Link to this post

    Hello Rob,

    We don't have a property which allows you to open RadMenuItem popup.
    The only workaround that I can suggest is to get the RadMenuItemAutomationPeer for this RadMenuItem and then invoke its Click method.
    You can get the automation peer like this:
    RadMenuItemAutomationPeer peer = FrameworkElementAutomationPeer.CreatePeerForElement(rootMenuItem) as RadMenuItemAutomationPeer;
    if(peer != null)
    {
       peer.Invoke();
    }

    I hope this will help you.

    Regards,
    Hristo
    the Telerik team
    Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
  3. DevCraft banner
Back to Top