C# 列表视图布局

C# 列表视图布局,c#,wpf,listview,C#,Wpf,Listview,这看起来很简单,但难以置信的难 我想要这样的布局: 它是ListView,应该占据按钮的最大高度(可见按钮的数量是动态的) 我试过这个 <Grid> <Grid.RowDefinitions> <RowDefinition Height="*"/> <RowDefinition Height="Auto"/> </Grid.RowDefinitions> ... &l

这看起来很简单,但难以置信的难

我想要这样的布局:

它是
ListView
,应该占据按钮的最大高度(可见按钮的数量是动态的)

我试过这个

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
        <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>

    ...

    <Grid Grid.Row="1">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>

        <StackPanel>
            <Button/>
            <Button/>
            ...
        </StackPanel>

        <ListView Grid.Column="1"/>

    </Grid>
</Grid>

...
...
问题:
ListView
有一个非常奇怪的行为,将self调整为内容。如果我添加了很多项,那么它会突然占用所有的窗口空间

问题:如何限制
ListView
高度不超过按钮总高度(
StackPanel
高度)

附言:mvvm,所以最好是纯xaml


附言:我觉得这将是一种某种东西与某种东西的结合。但是什么是什么呢?

您只需要将
堆栈面板的
实际高度
绑定到
列表视图.高度
,这样
列表视图
就不会比
按钮的
堆栈面板
大:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*" />
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>
    <Grid Grid.Row="1">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>
        <StackPanel Name="ButtonPanel" Grid.Column="0">
            <Button Content="Click" />
            <Button Content="Click" />
            <Button Content="Click" />
        </StackPanel>
        <ListView Grid.Column="1" ItemsSource="{Binding Tests}" 
            Height="{Binding ActualHeight, ElementName=ButtonPanel}" />
    </Grid>
</Grid>

您可以将较低内容网格的
MaxHeight
属性绑定到按钮
StackPanel
实际高度。此外,您还需要将按钮面板的垂直对齐设置为底部

这样,您可以在垂直方向上添加更多零件,但仍然保持按钮面板的高度

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
        <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>

    ...

    <Grid Grid.Row="1"
          MaxHeight="{Binding ElementName=ButtonPanel, Path=ActualHeight}">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>

        <StackPanel Name="ButtonPanel"
                    VerticalAlignment="Bottom">
            <Button Content="Button"/>
            <Button Content="Button"/>
            <Button Content="Button"/>
        </StackPanel>

        <ListView Grid.Column="1"/>

        <!-- Multiple controls can be added here,
             but the lower part will still stick
             to the size of the buttons panel -->
    </Grid>
</Grid>

...

将按钮和listview都放在自己的面板中,然后只需担心左右对齐?@Sayse,这为什么会有帮助?什么会阻止
ListView
使面板变大和成长?因为您将其高度限制为其所有者的大小,然后确保面板固定在height@Sayse,我还是不明白你的想法。注意:我不想固定大小。布局应该是动态的(任何大小的正常/最大化窗口)。可见按钮的数量将有所不同->
StackPanel
高度将有所不同->
ListView
应仅采用该高度,而不是更多。您又救了我!谢谢(是的,我为自己还不能独自解决这样的问题感到羞愧)我们生活和学习。在过去的某个时候,这也会难倒我那也行,谢谢。但它比@Sheridan的解决方案更阴暗(不那么明显)。