简体   繁体   English

如何从附加到按钮的菜单中获取所选项目

[英]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.

相关问题 如何通过打开上下文操作菜单从ListView中获取所选项目? (Xamarin.Forms) - How do I get the selected item from a ListView by opening the context actions menu? (Xamarin.Forms) 如何获得单选按钮所选项目的文本? - How do I get the text of the radio button selected item? 如何从上下文菜单中获取所选项目 - How to get the selected item from a context menu 如何从@ Html.DropDownListFor获取选定的项目 - How do I get the selected Item from @Html.DropDownListFor 如何从列表框到上下文菜单中获取选定的项目 - How to get selected Item from listbox to context menu 如何从Glass收到有关已选择自定义菜单项的通知? - How to get a notification from Glass that a custom menu item was selected? 如何从另一个表单按钮更改表单菜单项的文本 - How do i change text of form menu item from another forms button 如何获取所选菜单项的内容? - How to get the Content of selected Menu Item? 使用右键单击上下文菜单打开C#winform,但如何显示所选项? - C# winform opens with right-click context menu, but how do I get the selected item to show up? 如何单击按钮从微调器中删除所选项目? - How can I delete selected item from spinner on click button?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM