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.