Silverlight: Scrollviewer only appears when content overflows? - silverlight

I'm using Silverlight 4. I have a UserControl whose LayoutRoot is wrapped in a ScrollViewer. I'd like the scroll bar to only appear if the LayoutRoot overflows the page. It is possible to do it automatically, or should I write code to detect if the content will overflow and set the scrollbar visibility accordingly?

You should be able to do it automatically using the ScrollViewer.HorizontalScrollBarVisibility and ScrollViewer.VerticalScrollBarVisibility properties. Here's a list of all possible values for those properties (the ScrollBarVisibility enumeration):
Disabled
Auto
Hidden
Visible
I think "Auto" is what you're looking for:
Auto: A ScrollBar appears and the dimension of the ScrollViewer is applied to the content when the viewport cannot display all of the content. For a horizontal ScrollBar, the width of the content is set to the ViewportWidth of the ScrollViewer. For a vertical ScrollBar, the height of the content is set to the ViewportHeight of the ScrollViewer.
Hope this helps!

Related

How to make a WPF ScrollViewer use only the available width?

I have a wpf window, and there is a footer sections that can have multiple content which makes the width of the footer grows.
I set SizeToContent="WidthAndHeight" in my Window and when I add a lot of content to ScrollViewer, it grows accordingly, instead of just show the scrollbar.
It looks like ScrollViewer is taking into account its content, so how do I set the ScrollViewer to ignore it's content and only take the width available instead of make it grow?
thanks!
I suspect that by setting SizeToContent="WidthAndHeight" you tell the controls inside the window that they "can have all the space they want", so the ScrollViewer does not do anything becase it is not being restricted by its parent which is a necessary condition for a ScrollViewer to work (and make sense).
Try setting the MaxWidth property of the ScrollViewer. Once it has reached this width, it should start showing scrollbars.

What is the difference between a StackPanel and DockPanel in WPF?

What can a DockPanel do that a StackPanel cannot? If anyone has an image of something that can be achieved with a StackPanel, but not a DockPanel, than that would be great.
Stack Panel: The StackPanel, as the name implies, arranges content either horizontally or vertically. Vertical is the default, but this can be changed using the Orientation property. Content is automatically stretched based on the orientation (see screenshot below), and this can be controlled by changing the HorizontalAlignment or VerticalAlignment properties.
Dock Panel: The DockPanel is used to anchor elements to the edges of the container, and is a good choice to set up the overall structure of the application UI. Elements are docked using the DockPanel.Dock attached property. The order that elements are docked determines the layout.

how to use orientation in scroll viewer like scrollbar

how to use orientation in scroll viewer like scrollbar
I'm not sure what exactly you are trying to do, but the ScrollViewer has two properties called ScrollViewer.HorizontalScrollBarVisibility and ScrollViewer.VerticalScrollBarVisibility which you can set to Auto, Hidden, Visible and Disabled.
So if you want to show the vertical or horizontal scrollbar of the ScrollViewer you can set one of them to Visible.

How do I get content to realign around an expanding/collapsing WPF Expander?

Right now I have a Stackpanel, that contains a collapsed Expander, and a Listbox.
The Listbox beneath the collapsed Expander is being positioned as if the Expander was expanded. Is it possible for the StackPanel to dynamically do layout based on the collapsed size of the Expander, and then on the Expanded size of the Expander once it's been clicked on?
Thanks for your help.
After a bunch of documentation reading, I determined what my problem was.
If you explicitly set the height of the Expander (in a vertical layout) then it will always use that size in a StackPanel. If you set the height to "Auto" then it will use the contents of the Expander for the expanded size, and the collapsed size of the Expander when it's collapsed.

Dynamically resizing a window in Xaml

Is there a way for me to have the Window of my WPF application resize dynamically with the content? I tried setting the width and height to auto, but no go.
Set SizeToContent="WidthAndHeight" on the window and the window will dynamically resize based on its content size.
Use a resizable control like Grid and put all the controls in Rows/Columns. Also set HorizontalAlignment to stretch for each control inside the Grid.

Resources