如何获取在运行时添加的上下文菜单项中的文本?
我以 wpf 形式创建了一个上下文菜单。菜单项是在运行时添加的。 以下是添加上下文菜单的 XAML 代码片段:
<Window.ContextMenu>
<ContextMenu AllowDrop="True" VerticalContentAlignment="Top" Opened="note_Opened" >
<MenuItem Header="View Notes" Click="MenuItemViewNotes_Click" Name="mainMenu" />
...............................
</ContextMenu>
</Window.ContextMenu>
菜单项添加如下:
private void note_Opened(object sender, RoutedEventArgs e)
{
......... //some codes goes here
while(reader.Read()){
MenuItem newItem = new MenuItem();
newItem.Header = textEncrypt.DecryptString(reader[2].ToString());
// newItem.Click += new RoutedEventHandler(MenuItemViewNotes_Click);
mainMenu.Items.Add(newItem);
}
}
}
以下是“MenuItemViewNotes_Click”方法。我想获取单击的菜单项的相应索引。但是当我无法给出名称或索引时我在上面的方法中添加了该项目,有什么办法可以做到这一点吗?
private void MenuItemViewNotes_Click(object sender, RoutedEventArgs e)
{
mainMenu.Items.Clear();
MenuItem mi = sender as MenuItem;
if (mi!= null)
{
MessageBox.Show(mi.Header.ToString());
}
}
在这里我没有得到任何输出。我想这可能是处理上的问题。
下面是调用“note_opend”和“MainItemViewNotes_click”函数的xaml代码
<ContextMenu AllowDrop="True" VerticalContentAlignment="Top" Opened="note_Opened" >
<MenuItem Header="View Notes" Name="mainMenu" >
<MenuItem Header="note1" Click="MenuItemViewNotes_Click" IsCheckable="True"/>
</MenuItem>
<MenuItem Header="Settings" Click="MenuItemSettings_Click"/>
<MenuItem Header="Close" Click="MenuItemClose_Click"/>
</ContextMenu>
I have created a contextmenu in a wpf form.And menu items are added in the run time.
following is the XAML code snippet to add the context menu:
<Window.ContextMenu>
<ContextMenu AllowDrop="True" VerticalContentAlignment="Top" Opened="note_Opened" >
<MenuItem Header="View Notes" Click="MenuItemViewNotes_Click" Name="mainMenu" />
...............................
</ContextMenu>
</Window.ContextMenu>
menu items are added as following:
private void note_Opened(object sender, RoutedEventArgs e)
{
......... //some codes goes here
while(reader.Read()){
MenuItem newItem = new MenuItem();
newItem.Header = textEncrypt.DecryptString(reader[2].ToString());
// newItem.Click += new RoutedEventHandler(MenuItemViewNotes_Click);
mainMenu.Items.Add(newItem);
}
}
}
following is the "MenuItemViewNotes_Click" methods .I want to get the coresponding index of the clicked menu item.But I was unable to give name or index when I adding the item in the above method.Is there any way to do that?
private void MenuItemViewNotes_Click(object sender, RoutedEventArgs e)
{
mainMenu.Items.Clear();
MenuItem mi = sender as MenuItem;
if (mi!= null)
{
MessageBox.Show(mi.Header.ToString());
}
}
here I didn't get any out put.I think it may be some thing wrong in even handling.
again following is the xaml code where call to the "note_opend " and "MainItemViewNotes_click" functions
<ContextMenu AllowDrop="True" VerticalContentAlignment="Top" Opened="note_Opened" >
<MenuItem Header="View Notes" Name="mainMenu" >
<MenuItem Header="note1" Click="MenuItemViewNotes_Click" IsCheckable="True"/>
</MenuItem>
<MenuItem Header="Settings" Click="MenuItemSettings_Click"/>
<MenuItem Header="Close" Click="MenuItemClose_Click"/>
</ContextMenu>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否尝试过检查 e.Source 或 e.OriginalSource?您可能需要调试才能看到类型,但是对于上面的 RoutedEvents,它们应该包含您想要的源
have u tried to check e.Source or e.OriginalSource? you might need to debug to see the type, but for RoutedEvents like above, they should contain the source that you are after