WPF Stretch Middle Content in DockPanel or Stackpanel - wpf

How do I stretch the content in the middle section of a Stackpanel or Dockpanel. I know that a dockpanel will stretch the last item but I need to stretch the middle item. I know that I could use a grid with * for the middle section but I want to be able to set the left and/or right items as collapsed depending on my window size.

You should still use a DockPanel with LastChildFill setted.
Your DockPanel will stretch the last element you put in it, not the last one according to view.
So you can put the element you want to stretch in the last position and use DockPanel.Dock="Top/Bottom/Right/Left" on others one.

Related

Expander with GridSplitter

I've a grid splitter before to the expander(which exists in the right side of a wpf window) and when i was running the app i need to use grid splitter to see more data from expander after its expanded, but the problem is when i was drag splitter i'm getting full view of my expander irrespective of original width but after click on expander to collapse it won't go to its original position by maintaining the splitter dragged width. So any one help me how can i send back expander to its original position.
Thanks,
#nagaraju.
Once you have used the splitter the grid column widths will no longer be 'auto'.
One solution is to respond to the collapsed event of the expander and reset the width of the first column to auto using column.Width=GridLength.Auto;
As you are resizing the contents of the Expander it may make more sense to place the GridSplitter inside the Expander, and on the outside you just have a normal Grid which makes the cell where the Expander is size to content.

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.

WPF: Control that never causes children to NOT consume all available space

Right now I have a problem with StackPanels inside DockPanels. Often the StackPanel is taller than necessary for the contents, so the contents are stretched.
Is there something I can place in or around the StackPanel to mean "don't cause children to consume all avaialble space".
AFAIK the contents of the StackPanel do not "stretch" to fill it, anymore than any other control. i.e. the stretch to fill will be controlled by the HorizontalAlignment and VerticalAlignment of the child elements.
The default is usually Stretch. Try setting it to Left etc.
Well, what do you want your StackPanel to do? If you want it to only consume the visible space, use a grid. If you want to scroll the contents of your StackPanel, I believe you can put it in a ScrollViewer.

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