Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/265.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用ItemsSource时访问菜单项中的子项,C#\WPF_C#_Wpf_Itemssource - Fatal编程技术网

使用ItemsSource时访问菜单项中的子项,C#\WPF

使用ItemsSource时访问菜单项中的子项,C#\WPF,c#,wpf,itemssource,C#,Wpf,Itemssource,我有一个使用ItemsSource的菜单项 <MenuItem Name="Profiles" Header="Profiles list" Background="{x:Null}" FontSize="12" DisplayMemberPath="Name" ItemsSource="{Binding _profiles, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:MainW

我有一个使用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}}}">

在这种情况下,您可以使用父菜单项的单击事件访问单个菜单项:

    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
    }

然后您可以将item变量用于您的目的:)

有人能帮我吗?
    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
    }