tags:

views:

186

answers:

3

I have a ListView in WPF that is resizing my entire application window whenever I add items to it. I have the ListView size bound to the grid it is in, which is bound to the window size. So when the user resizes the window, the ListView grows. The problem is that when I add items to the ListView, it automatically stretches to try to fit the new content, which in turn stretches the size of the entire window.

Is there anyway to prevent this behavior from happening?

A: 

Have you tried setting the MaxWidth property?

cristobalito
I don't want to set a max width or height on it, because I do want it to be resized when someone resizes the window. I just don't want it to resize automatically when content is added to the ListView.
Kirk
Bind the ListViewItems MaxWidth to the ListView's MaxWidth?
cristobalito
I don't have a dev environment here but something along the lines below...<Style DataType={x:Type ListViewItem}> <Setter Property="MaxWidth" Value={RelativeSource Mode=Ancestor, Type=ListView, Path=MaxWidth} /></Style>
cristobalito
A: 

You can set the MaxHeight property for your ListView in your xaml, no?

VoodooChild
I don't want to set a max width or height on it, because I do want it to be resized when someone resizes the window. I just don't want it to resize automatically when content is added to the ListView.
Kirk
+1  A: 

You want to set the VerticalAlignment property to 'Stretch'. If necessary, in the parent control too, and so on, until you are in the Window control.

The caveat is that if one of the controls on the way up is a StackPanel, it will not stretch to fit, but ALWAYS expand to accommodate its content. So stay away from StackPanels, use Stretch, and you're set!

Remove your Bindings too, they are likely TwoWay: So the listview increasing its size is making your Window grow!

Hope that helps!

Kieren Johnstone