woodykiddy woodykiddy - 2 months ago 39
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%.

EDIT:

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

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


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

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

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="4*" />
        <ColumnDefinition />
    </Grid.ColumnDefinitions>

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

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.

Comments