views:

239

answers:

1

I am using a ListView with an ItemTemplate like this:

<Window.Resources>
    <DataTemplate x:Key="ItemTemplate">
        <WrapPanel Orientation="Horizontal">
            <Image Width="50" Height="50" Stretch="Fill" Source="{Binding Cover}"/>
            <Label Content="{Binding Title}" />
        </WrapPanel>
    </DataTemplate>
</Window.Resources>

But the Covers do not fill the screen like windows explorer windows.

How do I do this? They just get stacked vertically in my version.

alt text

+4  A: 

Try usinga WrapPanel as your ListView's item panel and disable the horizontal scrollbar:

<ListView ScrollViewer.HorizontalScrollBarVisibility="Disabled">
  <ListView.ItemsPanel>
    <ItemsPanelTemplate>
      <WrapPanel Orientation="Horizontal" />
    </ItemsPanelTemplate>
  </ListView.ItemsPanel>
  ...
</ListView>

Update: itowlson suggests this explanation to make things clearer: ItemTemplate specifies how each item should be rendered. It has no effect on how items are laid out. ItemsPanel, by contrast, does specify the layout.

Also, you may want all items to be displayed the same size. You can find out how to do that from this article: http://joshsmithonwpf.wordpress.com/2008/09/06/synchronizing-the-width-of-elements-in-an-itemscontrol/

Groky
And I might add that in this case you're probably better off using a regular ListBox and not a ListView.
Josh Einstein
Just to add a bit of explanation to this: ItemTemplate specifies how *each item* should be rendered. It has no effect on how items are laid out. ItemsPanel, by contrast, *does* specify the layout.
itowlson
Thanks, can I do this by setting ItemsPanelTemplate to my ItemTemplate directly? Like I don't want to paste my ItemTemplate inside here otherwise it looks more complicated. How can I just set the reference to that ItemTemplate like I did for ItemTemplate, but for ItemsPanelTemplate propery?
Joan Venge
I'm not sure I understand you, but no, the two templates are for different things. Read itowlson's explanation of the purpose of the two properties.
Groky
Ok sorry I thought they are the same WrapPanel. But when I did this now the icons are laid out horizontally on 1 line not like a square grid.
Joan Venge
Try setting HorizontalScrollBarVisibility="Disabled" on the ListView? does that help?
Groky
It says it cannot find that property on ListView.
Joan Venge
Sorry, the property is ScrollViewer.HorizontalScrollBarVisibility - updated the answer.
Groky
Thanks, that did the trick.
Joan Venge