Вопрос

I have a usercontrol containing a heading, a listbox and a button in the bottom.

When the listbox is filled with elements, I want the listbox to stretch to a maximium height, showing vertical scrollbars and the botton elements should be right below the listbox.

If someone resizes the window, the listbox should resize (respecting maxheight property) and show scrollbars when it needs to. I also want the save button to be right beneath the listbox and not in the bottom part of the usercontrol

I have found this to be really hard, once the listbox is filled with elements, it stretches out, no matter what.

<UserControl x:Class="WpfApplication1.UserControl1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:system="clr-namespace:System;assembly=mscorlib"
    Height="300" Width="525">
    <Grid >
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto" MaxHeight="150"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <TextBlock Grid.Row="0" Text="Heading"/>
        <Grid Grid.Row="1">
            <Grid.RowDefinitions>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
            <ListView Grid.Row="0" MaxHeight="100">
                <ListView.Items>
                    <system:Int32>0</system:Int32>
                    <system:Int32>0</system:Int32>
                    <system:Int32>0</system:Int32>
                    <system:Int32>0</system:Int32>
                    <system:Int32>0</system:Int32>
                    <system:Int32>0</system:Int32>
                    <system:Int32>0</system:Int32>
                </ListView.Items>
            </ListView>
        </Grid>
        <Separator Grid.Row="2" />
        <Button Grid.Row="3" Width="75" Content="Save"/>
    </Grid>
</UserControl>
Это было полезно?

Решение

Your original XAML seemed to be quite strange... I'm not surprised it wasn't working. For example, you set the ListView.MaxHeight property to 100, but also set the relevant RowDefinition.MaxHeight property to 150. Perhaps you meant to do that, I don't know... either way, you just needed to use the * setting for the Height property on the ListViews RowDefinition, set some VerticalAlignment properties and remove the inner Grid that did nothing useful. Try this:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="*" />
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>
    <TextBlock Grid.Row="0" Text="Heading" />
    <ListView Grid.Row="1" MaxHeight="100" VerticalAlignment="Top">
        <ListView.Items>
            <system:Int32>0</system:Int32>
            <system:Int32>0</system:Int32>
            <system:Int32>0</system:Int32>
            <system:Int32>0</system:Int32>
            <system:Int32>0</system:Int32>
            <system:Int32>0</system:Int32>
            <system:Int32>0</system:Int32>
        </ListView.Items>
    </ListView>
    <Separator Grid.Row="2" />
    <Button Grid.Row="3" Width="75" Height="25" Content="Save"
        VerticalAlignment="Top" />
</Grid>

UPDATE >>>

When working with Grids, just experiment with the "*" setting, which will take up all of the remaining space in the Grid. It's really not that difficult. Try this instead:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>
    <TextBlock Grid.Row="0" Text="Heading"/>
    <ListView Grid.Row="0" MaxHeight="100" VerticalAlignment="Top">
        <ListView.Items>
            <system:Int32>0</system:Int32>
            <system:Int32>0</system:Int32>
            <system:Int32>0</system:Int32>
            <system:Int32>0</system:Int32>
            <system:Int32>0</system:Int32>
            <system:Int32>0</system:Int32>
            <system:Int32>0</system:Int32>
        </ListView.Items>
    </ListView>
    <Separator Grid.Row="1" VerticalAlignment="Top" />
    <Button Grid.Row="2" Width="75" Height="25" Content="Save"
        VerticalAlignment="Top" />
</Grid>

UPDATE 2 >>>

Ok, so this should now be what you're after:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="*" MaxHeight="100" />
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>
    <TextBlock Grid.Row="0" Text="Heading" />
    <ListView Grid.Row="1" MinHeight="30" MaxHeight="100" VerticalAlignment="Top">
        <ListView.Items>
            <system:Int32>0</system:Int32>
            <system:Int32>0</system:Int32>
            <system:Int32>0</system:Int32>
            <system:Int32>0</system:Int32>
            <system:Int32>0</system:Int32>
            <system:Int32>0</system:Int32>
            <system:Int32>0</system:Int32>
        </ListView.Items>
    </ListView>
    <Separator Grid.Row="2" VerticalAlignment="Top" />
    <Button Grid.Row="3" Width="75" Height="25" Content="Save"
        VerticalAlignment="Top" />
</Grid>
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top