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.
The following code is just a proof of concept on how you could build your ContextMenu
. It will give you a ComboBox
as the content of 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>
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.
<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>
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);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.