C# 如何从上下文菜单中选择项目并将其显示在组合框中

C# 如何从上下文菜单中选择项目并将其显示在组合框中,c#,wpf,combobox,C#,Wpf,Combobox,我有一个组合框和它旁边的浏览按钮。组合框显示所有本地SQL实例,浏览按钮显示包含所有远程SQL实例的上下文菜单。我把这些都做完了。现在我需要从上下文菜单中选择一个项目,然后在组合框中将其显示为selecteditem 我认为这里至少涉及两个事件:选择上下文菜单中的MouseDown事件和组合框中的selection changed事件。这两个事件是一个挨着一个的。我怎样才能做到这一点。如何让一个事件触发另一个事件?我尝试了一些代码,但不起作用: <C

我有一个组合框和它旁边的浏览按钮。组合框显示所有本地SQL实例,浏览按钮显示包含所有远程SQL实例的上下文菜单。我把这些都做完了。现在我需要从上下文菜单中选择一个项目,然后在组合框中将其显示为selecteditem

我认为这里至少涉及两个事件:选择上下文菜单中的MouseDown事件和组合框中的selection changed事件。这两个事件是一个挨着一个的。我怎样才能做到这一点。如何让一个事件触发另一个事件?我尝试了一些代码,但不起作用:

                    <ComboBox Name ="comboSql"
                              Height="22"
                              Margin="10,0,20,0"
                              Width="250"
                              IsEditable="True"
                              Text="{Binding SelectedSqlServer, Mode=TwoWay}"
                              ItemsSource="{Binding LocalSqlServers}">

                    </ComboBox>
                    <Button x:Name="BrowseButton"
                            FontWeight="Bold"
                            Width="80"
                            Height="22"
                            Content="Browse"
                            Click="BrowseButton_Click">

                        <Button.ContextMenu>                               
                            <ContextMenu Name="BrowseButtonContext"
                                ItemsSource="{Binding RemoteSqlServers}"
                                         MouseDown="Select_Click">                               
                            </ContextMenu>
                        </Button.ContextMenu>
                    </Button>

   public string SelectedSqlServer
    {
        get { return selectedSqlServer.ToString(); }

    }

   public void Select_Click(object sender, RoutedEventArgs e)
    {
        selectedSqlServer = (System.Windows.Controls.ContextMenu)sender;
        comboSql.Text = selectedSqlServer.ToString();           
    }

公共字符串SelectedSqlServer
{
获取{return selectedSqlServer.ToString();}
}
公共无效选择\单击(对象发送者,路由目标e)
{
selectedSqlServer=(System.Windows.Controls.ContextMenu)sender;
comboSql.Text=selectedSqlServer.ToString();
}

以下是一些如何实现这一点的示例代码:

XAML:


我正在将contextmenustrip项的“文本”添加到组合框中。确保您有办法引用实际的SQL实例/连接字符串/正在使用的任何内容。

太好了!我要试一试。谢谢是否为ContextMenu内置了ItemClicked n事件?我在那里找不到它,所以出错了。这是为WPF。更新了我的答案与工作WPF的解决方案。没错,在WPF中没有为contextmenus单击任何项目。您需要为每个菜单项创建一个“单击”事件。我已经使用CommandParameter作为要传递的ConnectionString/DataSource/任何内容。享受吧!什么样的事件应该触发上下文菜单的选择?它没有单击事件。我尝试了MouseDown事件,但它没有触发。右键单击按钮会自动触发关联菜单
<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">

<Grid>
    <StackPanel>
    <ComboBox x:Name="cb1" Width="100" Height="40">

    </ComboBox>

        <Button x:Name="button1" Width="100" Height="40" Content="Browse">
            <Button.ContextMenu>
                <ContextMenu x:Name="context1">

                </ContextMenu>
            </Button.ContextMenu>
        </Button>
    </StackPanel>
</Grid>
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        SetUpContextMenu();
        SetUpComboBox();

    }

    private void SetUpComboBox()
    {
        cb1.Items.Add("Sql1");
        cb1.Items.Add("Sql2");
        cb1.Items.Add("Sql3");
    }


    private void SetUpContextMenu()
    {
        MenuItem item1 = new MenuItem();
        item1.Header = "Remote1";
        item1.Click += AddToComboBox;
        item1.CommandParameter = "Remote1";

        MenuItem item2 = new MenuItem();
        item2.Header = "Remote2";
        item2.Click += AddToComboBox;
        item2.CommandParameter = "Remote2";

        MenuItem item3 = new MenuItem();
        item3.Header = "Remote3";
        item3.Click += AddToComboBox;
        item3.CommandParameter = "Remote3";

        context1.Items.Add(item1);
        context1.Items.Add(item2);
        context1.Items.Add(item3);

    }

    public void AddToComboBox(object sender, RoutedEventArgs e)
    {
        MenuItem item = (MenuItem)sender;
        int index = cb1.Items.Add(item.CommandParameter);
        cb1.SelectedIndex = index;
    }
}