【发布时间】:2014-08-28 09:40:11
【问题描述】:
当我使用 ItemsControl 时,项目布局如下所示:
(来源:hostingpics.net)
因为我想选择项目,所以我考虑过使用 ListBox,但它给了我:
ListBox http://img4.hostingpics.net/pics/372874useOfListBox.png
ItemsControl 的代码:
<ItemsControl ItemsSource="{Binding AllFiles, Mode=OneWay}">
<ItemsControl.Resources>
<DataTemplate DataType="{x:Type viewModel:FolderVM}">
<DockPanel Margin="5" Height="70" Width="300">
<DockPanel.Resources>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="TextWrapping" Value="WrapWithOverflow"/>
</Style>
</DockPanel.Resources>
<Image Height="70" Width="70" DockPanel.Dock="Left" Source="{Binding Icon}"/>
<Grid Background="LightBlue" DockPanel.Dock="Left">
<WrapPanel Margin="10,0,0,0" Orientation="Vertical">
<TextBlock Text="{Binding Name}"/>
<TextBlock Text="File Folder"/>
</WrapPanel>
</Grid>
</DockPanel>
</DataTemplate>
<DataTemplate DataType="{x:Type viewModel:FileVM}">
<DockPanel Margin="5" Height="70" Width="300">
<DockPanel.Resources>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="TextWrapping" Value="WrapWithOverflow"/>
</Style>
</DockPanel.Resources>
<Image Height="70" Width="70" DockPanel.Dock="Left" Source="{Binding Icon}"/>
<Grid Background="LightBlue" DockPanel.Dock="Left">
<WrapPanel Margin="10,0,0,0" Orientation="Vertical">
<TextBlock Text="{Binding Name}"/>
<TextBlock Text="{Binding SizeFormatted}"/>
</WrapPanel>
</Grid>
</DockPanel>
</DataTemplate>
</ItemsControl.Resources>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Orientation="Vertical"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
对于列表框:
<ListBox ItemsSource="{Binding AllFiles, Mode=OneWay}">
<ListBox.Resources>
<DataTemplate DataType="{x:Type viewModel:FolderVM}">
<DockPanel Margin="5" Height="70" Width="300">
<DockPanel.Resources>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="TextWrapping" Value="WrapWithOverflow"/>
</Style>
</DockPanel.Resources>
<Image Height="70" Width="70" DockPanel.Dock="Left" Source="{Binding Icon}"/>
<Grid Background="LightBlue" DockPanel.Dock="Left">
<WrapPanel Margin="10,0,0,0" Orientation="Vertical">
<TextBlock Text="{Binding Name}"/>
<TextBlock Text="File Folder"/>
</WrapPanel>
</Grid>
</DockPanel>
</DataTemplate>
<DataTemplate DataType="{x:Type viewModel:FileVM}">
<DockPanel Margin="5" Height="70" Width="300">
<DockPanel.Resources>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="TextWrapping" Value="WrapWithOverflow"/>
</Style>
</DockPanel.Resources>
<Image Height="70" Width="70" DockPanel.Dock="Left" Source="{Binding Icon}"/>
<Grid Background="LightBlue" DockPanel.Dock="Left">
<WrapPanel Margin="10,0,0,0" Orientation="Vertical">
<TextBlock Text="{Binding Name}"/>
<TextBlock Text="{Binding SizeFormatted}"/>
</WrapPanel>
</Grid>
</DockPanel>
</DataTemplate>
</ListBox.Resources>
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
如何使用 ListBox 制作第一张图片中的布局?尝试了很多东西,但它不起作用。
我可以让 WrapPanel 有一个 MaxWidth,这样它就会停止水平堆叠,但我觉得这不是一件好事。
在您看来,我该怎么做?
编辑:在第一张图片中,我们没有看到 ItemsControl 中的所有项目
【问题讨论】:
标签: wpf listbox itemscontrol