oillio
oillio

Reputation: 4908

Set list items to grow to the width of the containing ListView - WPF

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?

Upvotes: 2

Views: 1174

Answers (3)

Maurizio Reginelli
Maurizio Reginelli

Reputation: 3212

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

Upvotes: 0

oillio
oillio

Reputation: 4908

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}"

Upvotes: 2

Henk Holterman
Henk Holterman

Reputation: 273244

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...

Upvotes: 1

Related Questions