Reputation: 268
I am working on a WPF application with layout similar to this defined in the following example 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>
Visually it's intended to look like this:
Simply put, the layout root is a grid that contains 3 columns; the left and right columns are just spacers and the middle column contains 2 User Controls (represented by 2 grids in my above example).
What I am trying to achieve is as follows:
If there is enough vertical space, I want the ItemsControl to display no scroll-bar and the button to appear right below the items (not at the bottom of its containing panel).
If there isn't enough vertical space, I want the ItemsControl to display a scroll-bar and not expand vertically effectively kicking the button out of view.
A visual example of the desired layout is as in the screenshot below:
Everything I have tried seems to either:
Is there any way I can achieve the desired layout (whether it's in XAML or code)?
Upvotes: 0
Views: 335
Reputation: 128013
Replace the StackPanel by a top-aligned Grid:
<!-- 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>
Upvotes: 1