WPF scrollbar for resizable window

This should be a very simple task, but for some reason I am facing a lot of problems with it in WPF.

Here is what I want: I have a bunch of controls in the window, including expander controls. I want to have scrollbars for this window when the content expands below the visible area. In addition, the window does not have a fixed width, it can be maximized, resized, etc.

I tried putting ScrollViewer as the first item in the window, but it does not work correctly. If I set the height and width to β€œAuto”, it does not scroll, and if I set it for special delays, it creates a window when the window is maximized.

Any help would be greatly appreciated!

+6
scroll wpf wpf-controls scrollviewer
source share
2 answers

I assume you have fixed width problems. If you provide a sample of your XAML, I can see if I can help further. The following actions do not appear in the field:

<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> 
+10
source share

You must set the HorizontalScrollBarVisibility parameter and the VerticalScrollBarVisibility parameter of the ScrollViewer parameter to Auto.

Here is an example:

 <Grid> <ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto"> <Canvas Width="400" Height="400"> <Button Canvas.Left="300">Left 300</Button> <Button Canvas.Top="300">Top 300</Button> </Canvas> </ScrollViewer> </Grid> 

This replaces the contents of the main window created by VS.

Run it and resize the window, enlarge it and scroll through the stripes that appear and disappear.

+8
source share

All Articles