WPF scrollbar for a resizable window
开发者_开发技巧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!
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>
You should set the HorizontalScrollBarVisibility and the VerticalScrollBarVisibility of the ScrollViewer 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 content of the main window generated by VS.
Run it and change the size of the window, maximize it and you'll scroll bars appearing and disappearing.
精彩评论