woodykiddy woodykiddy - 1 year ago 127
C# Question

How to resize WPF Listview proportionally?

I have a little bit problem with resizing Listview control. I intend to make it resize proportionally, according to the actual size of the window.

For initial size of Listview, it's 300 X 600 (width X height) in pixels. And I also set its maxHeight to 750, but its width stays the same, i.e. 300.

Also, in Wondow's properties, I have changed SizeToContent property to WidthAndHeight, as some of the threads suggest by doing so you let system decide the proper size of the control after window's been resized.

However, it's not yet working. So I am here to ask for help. Thanks.

P.S. is there anyway we can set percentage value for width and height in WPF? That'd be a lot easier if I am allowed to use percentage, like height = 80%.


To make it clearer, here is the general code structure in xaml

<Grid Height="Auto">
<RowDefinition Height="Auto" />
<RowDefinition Height="50" />
<ColumnDefinition Width="Auto" />
<StackPanel Grid.Row="0" Orientation="Horizontal" Height="Auto" Margin="0">
<ListView />
<StackPanel Grid.Row="1" Orientation="Horizontal" Height="Auto" Margin="0">
<Label />
<Button />

As you can see, I am currently using 2 stack panels and put them in separate rows. But Listview still can't resize proportionally even if I change to .

Answer Source

Place your ListView inside a Grid, and use the "*" width feature for the column:

        <ColumnDefinition Width="4*" />
        <ColumnDefinition />

    <ListView Grid.Column="0">...</ListView>

Column 0 in this example has a width of "4*" and column 1 has the default width of "1*". That means that between them they share a width of "five stars" and column 0 takes four of them. That gives you an 80% width.