Search code examples
wpfxamldocking

scroll viewer stuck to the right side of dockpanel?


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


Solution

  • 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.