我有一个usercontrol,其中包含一个标题,一个列表框和一个位于底部的按钮。
当列表框中填充了元素时,我希望列表框拉伸到最大高度,显示垂直滚动条,而botton元素应该在列表框的正下方。
如果有人调整窗口大小,列表框应调整大小(尊重maxheight属性)并在需要时显示滚动条。我还希望保存按钮位于列表框的正下方,而不是在usercontrol的底部
我发现这很难,一旦列表框中充满了元素,它就会延伸,无论如何。
<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>
答案 0 :(得分:0)
你原来的XAML似乎很奇怪......我并不感到惊讶它不起作用。例如,您将ListView.MaxHeight
属性设置为100
,但也将相关的RowDefinition.MaxHeight
属性设置为150
。也许你打算这样做,我不知道......不管怎样,你只需要对*
{{1} Height
ListView
属性使用RowDefinition
设置},设置一些VerticalAlignment
属性并删除没有任何用处的内部Grid
。试试这个:
<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>
更新&gt;&gt;&gt;
使用Grid
时,只需试用"*"
设置即可占用Grid
中的所有剩余空间。这真的不那么难。试试这个:
<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>
更新2&gt;&gt;&gt;
好的,所以现在应该是你想要的:
<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>