0

I'm trying to either create a template that will assign static values for the InputGestureText of up to the first 10 items of a MenuItem's ItemSource or do it from the code-behind after an event fires but I'm having no luck trying to get the actual MenuItem object.

I've been testing out different events being fired from the MenuItem but everytime I try to loop through the MenuItem's Items property, I'm getting string objects instead of the MenuItem object which I want in-order to assign a value to its InputGestureText.

Here's the XAML for the context menu:

<MenuItem x:Name="ClassNameContext_1" 
          Header="Suggested Names" 
          ItemsSource="{Binding Path=DataContext.ClassNameList, Source={x:Reference ImgView}}" 
          Click="ClassNameContext_Click"
          MouseEnter="ClassNameContext_MouseEnter"
          SubmenuOpened="ClassNameContext_1_SubmenuOpened"/>

Source Code for my attempts:

private void ClassNameContext_1_SubmenuOpened(object sender, RoutedEventArgs e)
        {
            MenuItem suggestedNames = e.OriginalSource as MenuItem;
            if (suggestedNames == null)
                return;

            suggestedNames.InputGestureText = "TEST";

            int index = 0;
            foreach (var item in suggestedNames.ItemContainerGenerator.Items)
            {
                MenuItem blah = item as MenuItem;
                var bloo = item.GetType();
                var bruh = suggestedNames.ItemContainerGenerator.ContainerFromIndex(index);
            }
        }

Basically up to the first 10 items will be assigned InputGestureTexts starting from F1 to F10.

EDIT

The reason why this is not a duplicate of the specified post is because the user there was working with statically created MenuItems whereas here it is being loaded from an ItemSource which can dynamically change given events that add or remove from the collection.

nwahs
  • 21
  • 3
  • So you are trying to enumerate before it was displayed? I think this is wrong, menu is just an addition to hotkeys, in other words window should always react on F1-F10 disregards if there is menu or not. Add those gestures to window, user control or whatever is the owner of that context menu. – Sinatr Mar 25 '19 at 14:44
  • Possible duplicate of [Defining MenuItem Shortcuts](https://stackoverflow.com/questions/4682915/defining-menuitem-shortcuts) – Sinatr Mar 25 '19 at 14:44
  • @Sinatr are you saying the menuitem isn't loaded which is why i'm not able to get the menuitem from the itemcollection? also how would I add it to the owner of the context menu when I'm trying to add the corresponding text for each existing item from the source collection? – nwahs Mar 25 '19 at 14:48
  • Well, you are using `ClassNameList` as source for sub-items. And it seems the items are simple string, this is why you get string when enumerating. If you would be asking about icon, then possible (MVVM) solution is to create a proper view model for each item with corresponding property defining icon and then use item template or triggers to set icon in xaml. But with input bindings that won't work. Input bindings has to be set on something what is already displayed and listening: e.g. to window containing that element with context menu or to its parent container (e.g. `Grid`). – Sinatr Mar 25 '19 at 14:57
  • See [this answer](https://stackoverflow.com/a/16924631/1997232), the input bindings are assigned to window. That's what I mean. You can use `CommandParameter` to bind F1-F10 to just one command and then obtain corresponding `ClassNameList` item by converting that parameter to index. – Sinatr Mar 25 '19 at 15:02
  • @Sinatr I understand that solution explains how to wire the command for the hotkeys but what I'm more focused on is displaying said keys in the InputGestureTexts for each existing item – nwahs Mar 25 '19 at 15:32

0 Answers0