0

I'm trying to recover the name of the menuitem where I clicked and I it always returns an empty string:

    private void MenuP_Click(object sender, RoutedEventArgs e)
    {
        MenuItem menu4 = e.OriginalSource as MenuItem;
        string ss = menu4.Name;         /// always empty. why?             
    } 

The XAML:

Window.Resources>
    <Style x:Key="ItemStyle" TargetType="MenuItem">
        <EventSetter Event="MenuItem.Click" Handler="MenuP_Click"></EventSetter>
    </Style>
    <Style x:Key="CatStyle">
        <Setter Property="MenuItem.ItemsSource" Value="{Binding XPath=submenu}"/>
        <Setter Property="MenuItem.DisplayMemberPath" Value="@es"></Setter>
        <Setter Property="MenuItem.ItemContainerStyle" Value="{StaticResource ItemStyle}"></Setter>
    </Style>
</Window.Resources>
<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="120"/> <ColumnDefinition Width="677*"/> 
    </Grid.ColumnDefinitions>

    <Menu Name="MenuP" VerticalAlignment="Top" ItemsSource="{Binding Source={StaticResource datos}, XPath=//menues/Menux}" DisplayMemberPath="@es" ItemContainerStyle="{StaticResource CatStyle}">

 </Grid>

the menuitems.count is always 0 too. How can I know which menuItem was clicked by the user?

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
user2367101
  • 91
  • 1
  • 6

1 Answers1

0

Try to get clicked MenuItem from sender parameter instead, and do type-casting as follow :

MenuItem menu4 = (MenuItem)sender;
string ss = menu4.Name;

This way of casting will throw exception if you cast wrong object instead of returning null like it's counterpart (... As MenuItem). That will make it clear in case you have wrong type-casting problem.

UPDATE :

I think you got empty string in ss because you never set Name property of MenuItem, at least I can't see that in the code posted. If you want to get string displayed in the MenuItem you can try this way instead :

MenuItem menu4 = (MenuItem)sender;
var xml = (XmlElement)menu4.DataContext;
var es = xml.Attribute["es"].Value;
har07
  • 88,338
  • 12
  • 84
  • 137
  • Thanks for your answer. It´s the same result. Its not null. when I move the mouse over the menu4 it reports: {System.Windows.Controls.MenuItem Header:System.Xml.XmlElement Items.Count:0} – user2367101 Mar 19 '14 at 14:22
  • @user2367101 updated my answer. I can't see where you set `Name` property, so I guess you want sth else : text displayed in `MenuItem` maybe? – har07 Mar 20 '14 at 01:34
  • Yes I want that text or something to identify the item clicked. Anything. Im using a XML file. I have this code : – user2367101 Mar 20 '14 at 22:13
  • About the update: I tried to set the name property with a Setter in the itemstyle: and it dont let me compile. It reports 2 errors: Error 2 Must have non-null value for 'Setter.Property'. Error 1 'Name' property cannot be set in the current element's Style. – user2367101 Mar 20 '14 at 22:17
  • how about the 2nd code snippet, does it work to get displayed text (value of `es` attribute of corresponding xml element)? – har07 Mar 21 '14 at 01:04