WPF scrollbar for a resizable window

Greg R picture Greg R · Feb 25, 2010 · Viewed 32.9k times · Source

This should be a very simple task, but for some reason I'm running into a lot of problems with it in WPF.

This is what I want to happen: I have a bunch of controls in a window, including expander controls. I want to have scroll bars for that window, when content expands below the visible area. Also, the window is not of fixed width, it can be maximized, resized, etc.

I tried putting a ScrollViewer as the first element in the window, but it's not working correctly. If I set the height and width to Auto, it doesn't scroll and if i set it to spefic detentions, it creates a box when the window is maximized.

Any help would be greatly appreciated!

Answer

bendewey picture bendewey · Feb 25, 2010

I'm assuming that you have some fixed width issues. If you provide a sample of your XAML I can see if I can help further. The following works without showing a box:

<Window x:Class="WpfSample1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <ScrollViewer>
        <StackPanel>
            <Rectangle Height="400" Width="400" Fill="Red" Margin="10" />
            <Rectangle Height="400" Width="400" Fill="Green" Margin="10" />
            <Rectangle Height="400" Width="400" Fill="Blue" Margin="10" />
            <Rectangle Height="400" Width="400" Fill="Yellow" Margin="10" />
        </StackPanel>
    </ScrollViewer>
</Window>