[英]How do I get the selected item from Menu attached to a button
In my .xaml, I had a button and when I left clicked it, a menu will be displayed for selection. 在我的.xaml中,我有一个按钮,当我单击它时,将显示一个菜单供选择。 Here's my .xaml code:
这是我的.xaml代码:
<Button x:Name="Btn" Content="Btn" HorizontalAlignment="Left" VerticalAlignment="Top"
Height="26" Width ="40" IsEnabled="True"
Click="Btn_Click" ContextMenuService.IsEnabled="False">
I disabled the ContextMenuService so that the menu wouldn't be able to display when I right clicked the button. 我禁用了ContextMenuService,以便右键单击按钮时无法显示菜单。
Below is my code to show the menu after I left clicked the button. 下面是我左键单击菜单后显示菜单的代码。 The name "Menu" referred to the context menu.
名称“菜单”指的是上下文菜单。
private void Btn_Click(object sender, RoutedEventArgs e)
{
ContextMenu contextMenu = this.FindName("Menu") as ContextMenu;
contextMenu.PlacementTarget = sender as Button;
contextMenu.IsOpen = true;
}
Is there any solution for returning the value from a context menu selection based on my situation? 有什么解决方案可以根据我的情况从上下文菜单选择中返回值吗?
I think what you need is a dropdown button , that is efficient fast and well composed. 我认为您需要的是一个下拉按钮 ,该按钮快速有效且结构合理。
You can have it in WPF Extended toolkit and a this Link will guide you there. 您可以在WPF扩展工具包中找到它,并且此链接将引导您到那里。
or a SplitButton in case you need to have button click as well as menu on it. 或使用SplitButton (如果需要单击按钮以及菜单)。
It sound like you want to change Modes or Properties in your context menu. 听起来好像您想在上下文菜单中更改“模式”或“属性”。 Therefore what i would have done is create a UserControl as you button and dropdown context menu, that will know each other.
因此,我要做的就是在按钮和下拉上下文菜单中创建一个UserControl,这将彼此了解。 When you change "mode" or something similar in your context menu so youll change it in the UserControl properties so that the button will also know it, and you can populate this for later use.
当您在上下文菜单中更改“模式”或类似内容时,您将在UserControl属性中对其进行更改,以便该按钮也将知道它,并且您可以填充它以备后用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.