wpf 如何将组合框添加为标签控件的 ContextMenu 项。WPF应用程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13157384/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add Combobox as ContextMenu Item for Label Control. WPF app
提问by SST
I want to add combobox as ContextMenu item for some label control in wpf application in my code behind. How can I do it? I searched a lot over web but nothing comes productive.
我想在后面的代码中为 wpf 应用程序中的某些标签控件添加组合框作为 ContextMenu 项。我该怎么做?我在网上搜索了很多,但没有任何成果。
采纳答案by SST
I got the solution, we can do this is folowing way:
我得到了解决方案,我们可以通过以下方式做到这一点:
ContextMenu contextmenu = new ContextMenu();
ComboBox CmbColorMenu = new ComboBox();
CmbColorMenu.ItemsSource = FontColors;// FontColors is list<objects>
CmbColorMenu.DisplayMemberPath = "Text";
contextmenu.Items.Add(CmbColorMenu);
回答by Eirik
The following code is just a proof of concept on how you could build your ContextMenu. It will give you a ComboBoxas the content of a MenuItem.
以下代码只是关于如何构建ContextMenu. 它会给你 aComboBox作为a的内容MenuItem。
<Label Content="label with context menu">
<Label.ContextMenu>
<ContextMenu>
<MenuItem Header="menu 1">
<ComboBox>
<ComboBoxItem Content="combo 1" IsSelected="True" />
<ComboBoxItem Content="combo 2" />
<ComboBoxItem Content="combo 3" />
</ComboBox>
</MenuItem>
</ContextMenu>
</Label.ContextMenu>
</Label>
回答by Colin Smith
Another alternative...this allows the ComboBox to appear directly when you do the Right-Click. Copy and paste this into KAXAML to see it working.
另一种选择...这允许组合框在您右键单击时直接出现。将其复制并粘贴到 KAXAML 中以查看其工作情况。
<Page
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid>
<Label Content="Some Label">
<Label.ContextMenu>
<ContextMenu>
<ContextMenu.Template>
<ControlTemplate>
<ComboBox SelectedIndex="0">
<ComboBoxItem>One</ComboBoxItem>
<ComboBoxItem>Two</ComboBoxItem>
<ComboBoxItem>Three</ComboBoxItem>
</ComboBox>
</ControlTemplate>
</ContextMenu.Template>
</ContextMenu>
</Label.ContextMenu>
</Label>
</Grid>
</Page>



