[英]How do I have an ItemsControl fit its contents and not expand vertically without regard to its sibling elements?
我正在使用WPF應用程序,其布局類似於以下示例XAML中定義的布局:
<Window>
<Grid x:Name="LayoutRoot">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="5*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Border Grid.Column="0" Background="AliceBlue" />
<!-- Main Panel -->
<Grid Grid.Column="1"
Background="LightPink">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<!-- Row 0: some User Control -->
<Grid Grid.Row="0" MinHeight="100" />
<!-- Row 1: some label -->
<Label Grid.Row="1" Content="Example text" />
<!-- Row 2: another User Control -->
<StackPanel Grid.Row="2">
<ScrollViewer VerticalScrollBarVisibility="Auto">
<ItemsControl>
<ItemsControl.Items>
<ContentPresenter Content="Item 1" />
<ContentPresenter Content="Item 2" />
<ContentPresenter Content="Item 3" />
<ContentPresenter Content="Item 4" />
<ContentPresenter Content="Item 5" />
<ContentPresenter Content="Item 6" />
</ItemsControl.Items>
</ItemsControl>
</ScrollViewer>
<Button Content="Open" />
</StackPanel>
</Grid>
<Border Grid.Column="2" Background="AliceBlue" />
</Grid>
</Window>
簡而言之,布局根是一個包含3列的網格。 左邊和右邊的列只是分隔符,中間的列包含2個用戶控件(在上面的示例中由2個網格表示)。
我要實現的目標如下:
如果有足夠的垂直空間,我希望ItemsControl不顯示滾動條,並且希望按鈕顯示在項目的正下方(而不是位於其包含面板的底部)。
如果沒有足夠的垂直空間,我希望ItemsControl顯示滾動條,而不是垂直擴展,從而無法有效地將按鈕踢出視野。
我嘗試過的一切似乎要么:
有什么方法可以實現所需的布局(無論是XAML還是代碼)?
用頂部對齊的網格替換StackPanel:
<!-- Row 2: another User Control -->
<Grid VerticalAlignment="Top" Grid.Row="2">
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<ScrollViewer VerticalScrollBarVisibility="Auto">
<ItemsControl>
...
</ItemsControl>
</ScrollViewer>
<Button Grid.Row="1" HorizontalAlignment="Left" Content="Open" />
</Grid>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.