What is the difference between a StackPanel and DockPanel in WPF? - 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.

Related

Silverlight: Scrollviewer only appears when content overflows?

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!

How to place a long Grid in a WPF Layout Container?

When I place a Datagrid in a WPF Grid Layout container, the datagrid lengthens the Grid Layout.
I want it to occupy only the space available on screen.
Make sure your Grid.ColumnDefinition or Grid.RowDefinition's Width or Height is not set to Auto. If you set it to a fixed size or a star size then the row/column should not resize.
If you're still having problems a code sample would be useful.
You should place your Datagrid into a ScrollViewer, and the ScrollViewer into your Grid cell.
the grid is placed inside a tabitem.
I tried the scrollviewer. but the only thing that happens:
the scrollviewer gets as large as the too long datagrid.
as the tabitem can vary in height, I would rather not like to specify a fixed height for the grid.

Stretching a WPF Canvas Horizontally

How do I make a Canvas stretch fully horizontally with variable width? This is the parent Canvas, so it has no parents, only children.
XAML Source: it displays in blend
http://resopollution.com/xaml.txt
Use a Grid as the top level element in your UI - it'll stretch to fill its container. Then put a Canvas with HorizontalAlignment="Stretch" inside the Grid and it'll behave the way you want.
<Grid xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Canvas Background="Blue"/>
</Grid>
That worked for me. The key is your top level UI element. While a Grid fills all available space by default, Canvases take up only as much room as their contents demand.
I'm guessing you've tried
canvas.HorizontalAlignment = HorizontalAlignment.Stretch
If this doesn't work, then what you could do is bind the Width and Height properties of the canvas to the ActualWidth and ActualHeight properties of the containing window.
You could use a dock panel to get it to fill the available width. The last item in a dock panel list of controls is automatically stretched to fill the remaining space.
<DockPanel>
<Canvas />
</DockPanel>
The canvas should do this automatically, unless you are manually setting the height and/or width. What kind of control are you trying to place the canvas on? Can you post your code?
The problem is that you're specifying the Height and Width. Without these properties, the control may appear to vanish in the designer, but it should size appropriately when you insert the canvas into another control.
If I recall correctly, the next version of WPF will have 'DesignWidth' and 'DesignHeight' properties that allow you to show the control in the designer with a given size without effecting it's measurement when inserted into other controls.

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.

WPF - setting HorizontalAlignment= Stretch to Textbox in StackPanel

Why doesn't a textbox stretch to fill space in a stackpanel? Is this by design? In a grid, the textbox stretches as expected.
Yes, it's by design. The StackPanel will allocate the space the TextBox asks for. If you haven't set a width on the TextBox, it will require only enough width to fit its text.
Kent's answer seems right.
To still force override the StackPanel behavior, I think you'd need to dynamically compute-set the Width property of the contained elements OR some funky override of MeasureOverride. I'd rather use another layout manager/panel. Some things I noted..
The default value for HorizontalAlignment and VerticalAlignment properties of child elements is Stretch (if you don't specify one explicitly).
The StackPanel will stretch elements based on its Orientation property value. So
Orientation=Horizontal means all elements will be vertically stretched to max. Elements flow horizontally.
Orientation=Vertical means all elements will be horiz stretched to max. Elements flow vertically.
Unless explicitly specified, Width and Height of child elements are NaN. If you specify an explicit value, StackPanel will honor them over the Horiz and Vert Alignment settings.
The StackPanel itself has HorizontalAlignment and VerticalAlignment that adds a further layout twist. You can experiment with this example.
StackPanel
The default value is stretch for both
HorizontalAlignment and
VerticalAlignment of content that is
contained in a StackPanel.
HorizontalAlignment
When Height and Width properties are
explicitly set on an element, these
measurements take higher precedent
during layout and will cancel the
typical effects of setting
HorizontalAlignment to Stretch.
I needed items to be sized evenly, but stacked vertically.
I used a UniformGrid, and set the Columns property to 1. (tested with a TextBox, and it stretches like you want)

Resources