C# 如何添加组合框作为标签控件的上下文菜单项。WPF应用程序

C# 如何添加组合框作为标签控件的上下文菜单项。WPF应用程序,c#,wpf,wpf-controls,contextmenu,C#,Wpf,Wpf Controls,Contextmenu,我想在我的代码隐藏中为wpf应用程序中的一些标签控件添加combobox作为ContextMenu项。我怎么做?我在网上搜索了很多次,但没有任何结果。以下代码只是关于如何构建上下文菜单的概念证明。它将为您提供一个组合框,作为菜单项的内容 <Label Content="label with context menu"> <Label.ContextMenu> <ContextMenu> <MenuItem

我想在我的代码隐藏中为wpf应用程序中的一些标签控件添加combobox作为ContextMenu项。我怎么做?我在网上搜索了很多次,但没有任何结果。

以下代码只是关于如何构建
上下文菜单的概念证明。它将为您提供一个
组合框
,作为
菜单项
的内容

<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>
        ContextMenu contextmenu = new ContextMenu();
        ComboBox CmbColorMenu = new ComboBox();
        CmbColorMenu.ItemsSource = FontColors;// FontColors is list<objects>
        CmbColorMenu.DisplayMemberPath = "Text";
        contextmenu.Items.Add(CmbColorMenu);

另一种选择…这允许在右键单击时直接显示组合框。将其复制并粘贴到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>
        ContextMenu contextmenu = new ContextMenu();
        ComboBox CmbColorMenu = new ComboBox();
        CmbColorMenu.ItemsSource = FontColors;// FontColors is list<objects>
        CmbColorMenu.DisplayMemberPath = "Text";
        contextmenu.Items.Add(CmbColorMenu);

一个
两个
三

我得到了解决方案,我们可以通过以下方式来实现:

        ContextMenu contextmenu = new ContextMenu();
        ComboBox CmbColorMenu = new ComboBox();
        CmbColorMenu.ItemsSource = FontColors;// FontColors is list<objects>
        CmbColorMenu.DisplayMemberPath = "Text";
        contextmenu.Items.Add(CmbColorMenu);
ContextMenu ContextMenu=新建ContextMenu();
ComboBox CmbColorMenu=新建ComboBox();
CmbColorMenu.ItemsSource=FontColors;//FontColor是一个列表
CmbColorMenu.DisplayMemberPath=“文本”;
contextmenu.Items.Add(CmbColorMenu);

谢谢Eirik..我想要类似的解决方案,但使用代码隐藏。。如何在代码隐藏中完成?如何在代码隐藏中完成?