2

I would like the items of my ListView to grow to take up the entire width of the available space within the ListView.

Currently, I have the item's width set like this (the ListView is named listView):

Width="{Binding ActualWidth, ElementName=listView, Mode=OneWay}"

The items will take up the width of the ListView. However, the vertical scroll bar is not taken into account. If the scroll bar is present, it is drawn on top of the right edge of the items.

Is it possible to fix this?

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
oillio
  • 4,748
  • 5
  • 31
  • 37
  • 1
    Not sure what you want here - isn't it the default for ListViewItems to use the full width? – H H Jul 27 '10 at 21:10
  • I am not using a ListViewItem, I am using an ItemTemplate to define the view of an ItemsSource binding. – oillio Jul 27 '10 at 21:35

3 Answers3

2

I solved this by referencing the ItemsPresenter directly. To do this, I needed to define a template for the Viewbox and name the ItemsPresenter.

The template looks like this:

<ListView.Template>
    <ControlTemplate TargetType="{x:Type ListView}">
        <Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="1" SnapsToDevicePixels="true">
            <ScrollViewer Focusable="false" Padding="{TemplateBinding Padding}">
                <ItemsPresenter x:Name="MLVItemsPresenter" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
            </ScrollViewer>
        </Border>
       <--SNIP-->
    </ControlTemplate>
</ListView.Template>

And the width binding looks like this:

Width="{Binding ActualWidth, ElementName=MLVItemsPresenter, Mode=OneWay}"
oillio
  • 4,748
  • 5
  • 31
  • 37
1

Just an idea, but what if you removed all that wWidth-setting?

When I use:

    <ListView DockPanel.Dock="Top">
        <ListViewItem Background="Orange">aaa</ListViewItem>
        <ListViewItem>aaa</ListViewItem>            
    </ListView>

the orange item spans the entire width of the ListView...

H H
  • 263,252
  • 30
  • 330
  • 514
  • I am relatively new to WPF, but in my experience this is not what happens. The orange item will only span the width required by it's contents. In this case, the width of 'aaa'. If the contents are larger than the available space, they will be asked to shrink. But if they are smaller, they will NOT be asked to expand (I would like them to expand). In my case, the list items are defined in a ItemTemplate so this may be why mine behaves differently. – oillio Jul 27 '10 at 21:33
  • I just checked. Yes, this is the behavior of a ListViewItem. But I am not using them, I am using an ItemTemplate. Is it possible to set the ItemTemplate to behave as a ListViewItem? – oillio Jul 27 '10 at 21:40
0

ActualWidth includes the scroll bar so I don't think there will be a way to fix that. What about using a ViewBox?

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
Maurizio Reginelli
  • 3,152
  • 2
  • 27
  • 41
  • How would I use a Viewbox? If I wrap my items in a Viewbox, all sorts of bad things will happen. I want to ask the items to grow to fit the avaliable space. The Viewbox allows the contents to use any space they desire, then it foribly expands or shrinks them to fit the avaliable space. It seems there should be some method to reference the width of the ItemsPresenter contained within the ScrollViewer inside the ListView. This should be the width I am looking for... – oillio Jul 27 '10 at 21:18