我正在尝试恢复单击的菜单项的名称,但它始终返回一个空字符串:
private void MenuP_Click(object sender, RoutedEventArgs e)
{
MenuItem menu4 = e.OriginalSource as MenuItem;
string ss = menu4.Name; /// always empty. why?
}
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>
menuitems.count 也始终为 0。我怎么知道用户点击了哪个 menuItem?