ContextMenu Add items where expand

2 posts, 0 answers
  1. Andrey
    Andrey avatar
    1 posts
    Member since:
    Oct 2012

    Posted 11 Oct 2012 Link to this post

    in XAML: 

    <telerik:RadContextMenu.ContextMenu>
        <telerik:RadContextMenu>
        <telerik:RadMenuItem Header="Sub Item 1"/>
                    <telerik:RadMenuItem Header="Sub Item 2" Style="{StaticResource RadContextMenuItemStyle}" ItemsSource="{Binding Groups}"
                                           MouseEnter="RadMenuItem_MouseEnter" Loaded="RadMenuItem_Loaded" Name="cont">
                        <telerik:RadMenuItem.ItemContainerStyle>
                            <Style       TargetType="telerik:RadMenuItem">
                                <Setter Property="Header"            Value="{Binding Name}" />
                                <Setter Property="IsSeparator"            Value="{Binding IsSeparator}" />
                                <Setter Property="CommandParameter" Value="{Binding Id}"/>
                            </Style>
                        </telerik:RadMenuItem.ItemContainerStyle>
                    </telerik:RadMenuItem>
                    <telerik:RadMenuItem Header="Item 2" ItemsSource="{Binding Groups}"/>
        </telerik:RadContextMenu>
                </telerik:RadContextMenu.ContextMenu>

    in xaml.cs:

            private void RadMenuItem_MouseEnter(object sender, MouseEventArgs e)
            {
                FillMethod();
            }


            private void FillMethod()
            {
                if (!f)
                {
                    this.Dispatcher.BeginInvoke(() =>
                                                    {
                                                        var viewmodel =
                                                            (MainPageViewModel)
                                                            this.Resources["ViewModel"];
                                                        viewmodel.PopupCommand.Execute(null);
                                                    });
                }
            }
            private bool f = false;
            private void RadMenuItem_Loaded(object sender, RoutedEventArgs e)
            {
                f = false;
            }

    in viewmodel:
            protected virtual void InvokePopup(object obj)
            {
                Groups = new List<Group>();
                Groups.Add(new Group {Name = "Loading...",Id = "---"});
                fillservice.GetGroups();
            }


            public MainPageViewModel()
            {
                fillservice = new MockFill();
                fillservice.Completeevent += FillContextMenuMethod;
                ActionCommand = new DelegateCommand<object>(InvokeAction,CanInvokeAction);
                PopupCommand = new DelegateCommand<object>(InvokePopup, CanInvokePopup);
                Groups = new List<Group>();
                Groups.Add(new Group {Name = "Loading...",Id = "---"});

            }


            private void FillContextMenuMethod(object sender, GroupsEventArgs e)
            {
                UIDispatcher.BeginInvoke(() =>
                {
                    Groups = e.Groups.ToList();
                });
            }

    Context menu have a bad behavior, where i change my property group where, submenu is expanding(all arrows had collapsed
    for 1-3 seconds and submenu collapsed)
  2. Rosen Vladimirov
    Admin
    Rosen Vladimirov avatar
    640 posts

    Posted 16 Oct 2012 Link to this post

    Hi Andrey,

    We have tried such scenario and we have set a solution for you. The problem is that if you try to remove all items in the collection, the RadContextMenu will close the popup as it doesn't have what to show. Meanwhile you are adding some items and this is causing the confusion.
    In our simple application we are using Dispatcher to add items dynamically, but we remove all not needed items after we've added the new Groups.

    Please check the attachment and inform us if you still have any concerns.

    All the best,
    Rosen Vladimirov
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

  3. DevCraft banner
Back to Top