0

I have a stretch problem with my scroll viewer, I have a textbox and button inside a dock panel along with my scrollviewer, I wanted the scroll viewer underneath those items but stretched to the width of the dock panel and height stretched from the bottom of the dock panel up to the button and textbox.

So I tryed this:

<UserControl x:Class="WpfApplication4.AppPages.FindStudent"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300" Loaded="UserControl_Loaded">
    <DockPanel Height="299" Width="289">
        <TextBox Height="23" Name="textBox1" Width="188" VerticalAlignment="Top"/>
        <Button Content="Button" Height="23" Name="button1" Width="100" Click="button1_Click" VerticalAlignment="Top" />
        <ScrollViewer VerticalScrollBarVisibility="Hidden" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Width="88">

However the scroll viewer is somehow stuck to the right hand side like so:

enter image description here

Paolo Moretti
  • 54,162
  • 23
  • 101
  • 92
Kirsty White
  • 1,210
  • 3
  • 26
  • 54

2 Answers2

1

You'll need to tell the items how to dock using the attached property DockPanel.Dock.

Something like this:

<DockPanel>
    <StackPanel DockPanel.Dock="Top" Orientation="Horizontal">
        <TextBox />
        <Button />
    <StackPanel>
    <ScrollViewer DockPanel.Dock="Top" />
</DockPanel>

Note that by default the last child of the DockPanel will fill remaining available space.

Jay
  • 56,361
  • 10
  • 99
  • 123
  • On the DockPanel you can set LastChildFill = "False" if you don't want the last control to stretch to fill the remaining space. – Dan Busha Apr 18 '12 at 15:31
1

I'll echo what Jay said, you'll need to specify how you want the controls to dock inside of your DockPanel by setting DockPanel.Dock. Some additional notes:

  • LastChildFill will control whether the last element stretches to fill the remaining space
  • The default docking is to the left
  • The order in which the controls are listed will affect how they are docked.

See the MSDN documentation here, and a simple tutorial here.

Dan Busha
  • 3,723
  • 28
  • 36