[英]How to select a item from a context menu and displayed it a combobox
I have a combobox and a browse button next to it. 我旁边有一个组合框和一个浏览按钮。 The combobox display all local SQL instances and the browse button displays a context menu with all remote SQL instances.
组合框显示所有本地SQL实例,浏览按钮显示包含所有远程SQL实例的上下文菜单。 I got all these done.
我完成了所有这些。 Now I need to select an item from the context menu and then display it in the combobox as a selecteditem.
现在,我需要从上下文菜单中选择一个项目,然后将其作为选定项显示在组合框中。
I think there are at least two events involved here: MouseDown event in context menu for the selection and selection changed event from the combobox. 我认为这里至少涉及两个事件:上下文菜单中的MouseDown事件,用于选择和组合框中的选择更改事件。 And these two events are one next to each other.
这两个事件是彼此相邻的。 How can I achieve it.
我该如何实现。 How do I have one event to trigger another?
如何让一个事件触发另一个事件? I tried some code but it doesn't work:
我尝试了一些代码,但是没有用:
<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();
}
Here is some sample code for how to accomplish this: 这是一些有关如何实现此目的的示例代码:
XAML: XAML:
<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>
CodeBehind: 代码隐藏:
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;
}
}
I'm adding the "Text" of the contextmenustrip item to the combobox. 我将contextmenustrip项目的“文本”添加到组合框。 Make sure you have a way to reference your actual SQL instance/connection string/whatever you're using.
确保您有一种方法可以引用实际的SQL实例/连接字符串/所使用的内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.