WPF:多列列表框/列表视图?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17087640/
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
WPF: multi-column listbox / listview?
提问by Impworks
I'd like to create a list in WPF that displays data in several columns, like Explorer display a list of files in 'Small icons' view mode:
我想在 WPF 中创建一个列表,在几列中显示数据,例如资源管理器在“小图标”视图模式下显示文件列表:


Each item must be represented by a DataTemplateand the scrolling is supposed to be horizontal. How do I make such a list?
每个项目必须由 a 表示DataTemplate并且滚动应该是水平的。我如何制作这样的清单?
回答by dkozl
You need to change ItemsPanelof your ListBoxto WrapPanelwith vertical Orientationand disable vertical scroll bar on your ListBoxSomething like this:
你需要改变ItemsPanel你的ListBox对WrapPanel与垂直Orientation您和禁用垂直滚动条的ListBox事情是这样的:
<ListBox ScrollViewer.VerticalScrollBarVisibility="Disabled" ItemsSource="{Binding=MyItems}">
<ListBox.ItemTemplate>
<DataTemplate>
<!--my item template-->
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Orientation="Vertical"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
回答by ar.gorgin
回答by JSJ
try something like below.
尝试类似下面的内容。
<ListView ItemsSource="{Binding Files}" >
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Width="{Binding (FrameworkElement.ActualWidth), RelativeSource={RelativeSource AncestorType=ScrollContentPresenter}}"
ItemWidth="{Binding (ListView.View).ItemWidth, RelativeSource={RelativeSource AncestorType=ListView}}"
MinWidth="{Binding ItemWidth, RelativeSource={RelativeSource Self}}"
ItemHeight="{Binding (ListView.View).ItemHeight, RelativeSource={RelativeSource AncestorType=ListView}}" />
</ItemsPanelTemplate>
</ListView.ItemsPanel>
<ListView.View>
<GridView >
<GridView.Columns>
<GridViewColumn DisplayMemberBinding="{Binding Name}" />
</GridView.Columns>
</GridView>
</ListView.View>
</ListView>
code behind sample.
样本后面的代码。
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
Files = new ObservableCollection<FileInfo>();
var files = new System.IO.DirectoryInfo("C:\Windows\").GetFiles();
foreach (var item in files)
{
Files.Add(item);
}
this.DataContext = this;
}
public ObservableCollection<FileInfo> Files { get; set; }
}

