4

I have MenuItem that uses ItemsSource.

<MenuItem Name="Profiles" Header="Profiles list" Background="{x:Null}" FontSize="12" DisplayMemberPath="Name" 
    ItemsSource="{Binding _profiles, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:MainWindow}}}">

When I try to access an item I get an item from ItemsSource... How can I get access in codebehind to MenuItems in foreach like below?

foreach (MenuItem mi in Profiles.Items)
SkyDancer
  • 129
  • 1
  • 4
  • 13

1 Answers1

0

you can get access to individual menuitems using parent menuitem's click event in this case:

    Profiles.Click += profileClick;
    private void profileClick (object sender, RoutedEventArgs e) {
         MenuItem item = e.OriginalSource as MenuItem;
         //you can use item.Name and item.Header to identify the MenuItem
    }

then you can use the item variable for your purpose :)

pradeep
  • 25
  • 1
  • 10