How to bring up a box (listbox) from a button click

时间:2015-06-30 19:19:01

标签: c# wpf

I am doing this project that needs to list all SQL local and remote servers. Because to find remote servers takes time so I am listing the local servers in the dropdown combobox so users can select an instance from there, which is most of the cases. I have no problem for this part. However, I need to list all remote servers also so I think a smart way to do it is to through a browse button right next to the combo box, so users can click on it if they want to select a remote SQL instance. I can find all remote SQL instances without problems and put them in a ObservableCollection variable. Now the problem is that how I can bring up the listbox, which has all remote server names in it? I assume there are two parts of code that I need: one for xaml and one is the C# code behind for the click event. Any help is greatly appreciated!

1 个答案:

答案 0 :(得分:0)

我想出了如何从一个按钮带来上下文菜单:

这是xaml:      

int[] arr = new int[20];
int startIndex = Math.random()*arr.length;


for(int i = startIndex; i < startIndex+4; i++) {
    System.out.println(arr[i%arr.length]);
}

以下是代码:

                        <Button.ContextMenu>

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

                            </ContextMenu>
                        </Button.ContextMenu>
                    </Button>