当使用ItemsSource时,可以访问MenuItem中的子项,c# WPF
本文关键字:WPF MenuItem ItemsSource 访问 | 更新日期: 2023-09-27 17:50:18
我有MenuItem使用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}}}">
当我尝试访问一个项目时,我从ItemsSource获得一个项目…我怎样才能在代码后面访问菜单项在foreach如下?
foreach (MenuItem mi in Profiles.Items)
您可以使用父菜单项的单击事件访问单个菜单项,在这种情况下:
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变量:)