wpf ListBox vs. ListView - 如何选择数据绑定

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/227231/
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-08 19:43:56  来源:igfitidea点击:

ListBox vs. ListView - how to choose for data binding

wpfdata-bindinglistviewlistbox

提问by Geoff Cox

I'm considering either a ListBox or a ListView for a WPF application. It seems either supports data binding and item templates. My application has a simple list of items that I intend to be able to search/sort/filter based on user input. The data binding demo (http://msdn.microsoft.com/en-us/library/ms771319.aspx) uses a ListBox with a CollectionViewSource.

我正在考虑为 WPF 应用程序使用 ListBox 或 ListView。似乎支持数据绑定和项目模板。我的应用程序有一个简单的项目列表,我打算根据用户输入搜索/排序/过滤这些项目。数据绑定演示 ( http://msdn.microsoft.com/en-us/library/ms771319.aspx) 使用带有 CollectionViewSource 的 ListBox。

Does anyone have pros/cons for which control to use and when?

有没有人有使用哪种控件以及何时使用的优​​点/缺点?

回答by Philip Rieck

A ListView is a specialized ListBox (that is, it inherits from ListBox). It allows you to specify different views rather than a straight list. You can either roll your own view, or use GridView (think explorer-like "details view"). It's basically the multi-column listbox, the cousin of windows form's listview.

ListView 是一个专门的 ListBox(也就是说,它继承自 ListBox)。它允许您指定不同的视图而不是直接列表。您可以滚动自己的视图,也可以使用 GridView(想想类似资源管理器的“详细信息视图”)。它基本上是多列列表框,是 windows 窗体列表视图的表亲。

If you don't need the additional capabilities of ListView, you can certainly use ListBox if you're simply showing a list of items (Even if the template is complex).

如果您不需要 ListView 的附加功能,如果您只是显示项目列表(即使模板很复杂),您当然可以使用 ListBox。