Prevent ListView from Resizing Window When Adding Items WPF/C#
Asked Answered
S

6

6

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?

Sal answered 26/6, 2010 at 22:48 Comment(0)
M
5

Change SizeToContent of your window to Manual or Width (no Height should be there). This would prevent window itself from changing its height automatically according to content.

Maryrose answered 6/8, 2011 at 12:25 Comment(0)
C
1

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!

Chiliad answered 27/6, 2010 at 11:41 Comment(0)
M
1

You can use the following code.

First get the value of listview onload. Then store it in a variable, then do this code in the ColumnWidthChanging event property like this:

     e.cancel = true;
     e.NewWidth = // the declared variable in which you store the list view with value in the onload function of the form

 int a = 100;
    e.cancel =true;
    e.NewWidth = a;
Margotmargrave answered 24/8, 2011 at 2:30 Comment(0)
D
0

Have you tried setting the MaxWidth property?

Dashiell answered 26/6, 2010 at 22:52 Comment(3)
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.Sal
Bind the ListViewItems MaxWidth to the ListView's MaxWidth?Dashiell
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>Dashiell
D
0

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

Domeniga answered 26/6, 2010 at 22:53 Comment(1)
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.Sal
S
0

10 years later...

  1. Put the ListView inside a flexible control which resizes as you resize the window, for example, a Border. This way, when the Window resizes, the Border resizes, and when the Border resizes, the ListView resizes.

  2. Bind the MaxHeight property of the ListView to the ActualHeight property of the Border. This way, the ListView cannot resize the Border.

For details on how to achieve this, see this answer (to a practically duplicate question which is also about 10 years old): https://mcmap.net/q/1777222/-how-to-prevent-a-listview-from-expanding-the-window-dimension

Saga answered 23/7, 2021 at 12:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.