Xaml - Vertical scrollbar in stackpanel - wpf

I have creating a wpf application and in my settings panel I have tons of UI elements. The problem is that when I resize the window some of these elements are not visible anymore. Is there any way to add a simple vertical scrollbar?
I have tried this below and add my content into it :
<ScrollViewer VerticalScrollBarVisibility="Auto">
<Grid>
<StackPanel>
//Content
</StackPanel>
</Grid>
</ScrollViewer>
I'm not sure if I put the ScrollViewer to the right Place but I got this error :
The member resources is not recognized or is not accessible
and for this error I have tried to replace the Page.Resources with Window.Resources but it did not help.
Anyways how could I get my vertical scrollbar working? Any helps?

Problem solved by removing the Width and Height properties from the Page.
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="800" Width="1400"
WindowTitle="ScrollViewer Sample">
<ScrollViewer VerticalScrollBarVisibility="Auto">
<Grid>
<StackPanel>
//Content
</StackPanel>
</Grid>
</ScrollViewer>
</Page>

You should get rid of the StackPanel. A StackPanel measures its children with an infinite space and therefore it doesn't work very well with scroll bars:
Horizontal scroll for stackpanel doesn't work

Related

ScrollViewer inside (Stack-)Panel

I'm fighting with a ScrollViewer inside a StackPanel. The ScrollViewer only shows one scrollbar depending on the StackPanel's orientation, what I kind of understand as the StackPanel thinks to be unlimited in that direction. I therefore tried to limit the size of the ScrollViewer by binding it to the StackPanel's width and height. When the application is brought up it shows both scrollbars but they do not resize properly. What is wrong or how should I do it.
(Remark: I know I can use a Grid instead of the StackPanel and the ScrollViewer behaves as expected. However once I place that Grid into a StackPanel the problem shows up again.)
<Window x:Class="tt_WPF.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="500" Width="500">
<StackPanel x:Name="sp" Orientation="Horizontal">
<ScrollViewer
HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto"
Width="{Binding ElementName=sp, Path=ActualWidth}"
Height="{Binding ElementName=sp, Path=ActualHeight}">
<Button Background="LightCoral" Width="500" Height="500">Hey</Button>
</ScrollViewer>
</StackPanel>
A Scroll viewer is useless inside a vertical StackPanel. A vertical StackPanel has its height set to infinity, so the ScrollViewer has all the size it wants and will never show the scrollbar. You should switch to a Grid or DockPanel.
I had the same problem, i solved it by switching stackpanel and scrollviewer.

Content scrolling with DockPanel

I'm facing problem with scrolling content in Dock panel .
My controls placed in 'DockPanel' as below
<DockPanel>
<ScrollViewer>
<StackPanel>
<!-- Here controls are like Radiobutton,Lable ,CheckBox,Textblock are added dynamically in grid.-->
</StackPanel>
</ScrollViewer>
I'm using only vertical scrollbar, not need horizontal scrollbar,
When I first time traverse through controls in 'DockPanel' by using tab ,Tab focus goes off the screen but panel is not scrolling down.
Please help me out I'm really stuck over here.
Thanks in advance.
Add IsTabStop="True" in Scrollviewer:
<DockPanel>
<ScrollViewer IsTabStop="True" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
<StackPanel></StackPanel>
</ScrollViewer>
</DockPanel>

ScrollViewer with SizeToContent = WidthAndHeight

I have MyWindow where SizeToContent="WidthAndHeight" in WPF. When MyWindow is bigger than screen I want to activate a ScrollViewer. What is the easiest way to do that?
you need to use a... Scrollviewer.
some code for you:
<Window x:Class="Test.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow"
Name="Window"
SizeToContent="WidthAndHeight">
<ScrollViewer HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Background="Green">
<Grid Name="MainGrid" Background="red" MinWidth="600" MinHeight="400">
<!--Some Content Here-->
<Label>Foo</Label>
</Grid>
</ScrollViewer>
</Window>
What this code does:
A Window with SizeToContent=WidthAndHeight, as wanted.
A ScrollViewer that stretches with the window
A Grid to put your content (could be a Canvas, or any kind of Panel). This grid as MinWidth and MinHeight set so that what is inside the grid will be able to stretch up but not down. So your content will initially be 600x400 (in this case) but will be able to stretch up. If you try to size it down to 300x200 for instance, you'll get your scrollbars.
this should at least get you started.

WPF Horizontal Scrollbar is not visible

I have a Grid inside ScrollViewer. Grid has only one columns and many rows. I add controls into grid rows. The problem is I can see VerticalScrollBar when I am adding controls but I cannot see HorizontalScrollBar although I add very wide controls.
My xaml looks like the following
<ScrollViewer>
<Grid>
rows ...
</Grid>
</ScrollViewer>
Thanks,
ant.
Set the HorizontalScrollBarVisibility of the ScrollViewer to "Auto" or "Visible".
<ScrollViewer HorizontalScrollBarVisibility="Auto">
<Grid>
... rows ...
</Grid>
</ScrollViewer>

Silverlight 2.0 - scroll vertically, wrap horizontally

In silverlight 2.0. I have some content that i want to scroll vertically and wrap horizontally. In the controls I have a dock panel. The DockPanel's last child, which fills it, is a ScrollViewer
<UserControl x:Class="MyProject.MyControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:WinControls="clr-namespace:Microsoft.Windows.Controls;
assembly=Microsoft.Windows.Controls"
Width="400" Height="300">
<WinControls:DockPanel LastChildFill="True">
...
<ScrollViewer x:Name="MessageScroll" HorizontalScrollBarVisibility="Hidden"
VerticalScrollBarVisibility="Auto" BorderThickness="0" >
<Controls:TextDisplay x:Name="TextDisplay"></Controls:TextDisplay>
</ScrollViewer>
The TextDisplay control XAML looks like this:
<UserControl x:Class="MyProject.TextDisplay"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<TextBlock x:Name="TextDisplayText" TextWrapping="Wrap">
</TextBlock>
</UserControl>
What I want to happen: The TextDisplay should occupy the main area of the control,
with a vertical scrollbar if the height doesn't fit. The messages should wrap when they get too long horizontally.
The scrolling works, but now the messages don't wrap at the right-hand edge. they just cut off. It's not constraining the width, just hiding the HorizontalScrollBar. If I set HorizontalScrollBarVisibility="Auto" I can see them scrolling off to the right. How do i force it to wrap?
Try setting the HorizontalScrollBarVisibility of the ScrollViewer to Disabled (or do not specify a value as Disabled is the default) then the TextDisplay will wrap correctly and the horizontal scroll bar will not be displayed.

Resources