WPF 下拉列表框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27134081/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 12:43:48  来源:igfitidea点击:

WPF Dropdown ListBox

c#wpf

提问by Gregor Menih

I'm trying to create a ListBox, with a dropdown capability, but notComboBox. Although, if ComboBox can work this way, I don't mind.

我正在尝试创建一个ListBox具有下拉功能的 , 但不是ComboBox。虽然,如果 ComboBox 可以这样工作,我不介意。

I've got a Button, and a TextBlockbelow it:

我有一个ButtonTextBlock下面有一个:

button and textblock

按钮和文本块

The user can select multiple files, which are then added to a List. When the "files selected" TextBlock is clicked, I want to show a drop-down, showing the names of the files, and a button to remove a file from selection next to the name, which I can best describe as a Dropdown ListBox.

用户可以选择多个文件,然后将这些文件添加到List. 当单击“选择的文件”TextBlock 时,我想显示一个下拉列表,显示文件的名称,以及一个按钮以从名称旁边的选择中删除文件,我最好将其描述为 Dropdown ListBox

I've tried using a ComboBoxwith DataTemplate. The only drawback was that I don't like how it looks, and that it selects and item (e.g. changes the "# files selected" text), if I click anything but the remove button.

我试过使用ComboBoxwith DataTemplate。唯一的缺点是我不喜欢它的外观,如果我点击除删除按钮之外的任何东西,它会选择和项目(例如更改“# 选择的文件”文本)。

采纳答案by Justin Pihony

All a combobox is is a panel, so just create your own panel with the controls that you need and display it below the textbox when it is clicked. Don't worry about trying to reuse an existing control if it doesn't work for you. That's what makes WPF so great; it can be molded to your needs

组合框只是一个面板,因此只需使用您需要的控件创建您自己的面板,并在单击时将其显示在文本框下方。如果它不适合您,请不要担心尝试重用现有控件。这就是 WPF 如此出色的原因;它可以根据您的需要进行塑造