WPF <StatusBar> is not positioned at the bottom of the window - wpf

We have a WPF executable that creates a and then dynamically loads several assemblies. Each assembly represents a screen (.xaml) that is displayed in one of the tabs. The Problem is that the is right under the and not at the bottom of the window. How do I force the to always be at the bottom of the
window? Thx!
UserControl
DockPanel
CheckBox
StatusBar
DockPanel
UserControl

In addition to ArsenMkrt's answer about including the DockPanel.Dock="Bottom" attribute, don't forget that the LAST element in a DockPanel will fill the area unless you explicitly tell it otherwise using a height command (regardless of the DockPanel.Dock attribute provided).
my suggestion is to do thus:
<UserControl>
<DockPanel>
<StatusBar DockPanel.Dock="Bottom" />
<CheckBox />
</DockPanel>
</Usercontrol>

I had the same problem just now. Thanks to Stephen Wrighton's tip that the last element added to a DockPanel fills the area left over, I figured out how to set up my Window. It was a bit weird since I added the Grid last but it was positioned in the middle.
<Window>
<DockPanel>
<Menu DockPanel.Dock="Top">
<MenuItem Header="_File">
</MenuItem>
</Menu>
<StatusBar DockPanel.Dock="Bottom">
<StatusBarItem Content="Filler" />
</StatusBar>
<Grid x:Name="rootGrid">
</Grid>
</DockPanel>
</Window>

Did you try?
<StatusBar DockPanel.Dock="Bottom" ... />

In my case adding DockPanel.Dock="Bottom" was not enough I was forced to add VerticalAlignment="Bottom" as well.
<StatusBar DockPanel.Dock="Bottom" VerticalAlignment="Bottom" />

<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<DockPanel Grid.Row="0"/>
<StackPanel Grid.Row="1">
<StatusBar>Status Text</StatusBar>
</StackPanel>

If a StatusBar control in WPF is not positioned at the bottom of the window, there are several potential causes for this issue:
The StatusBar is not placed inside the bottommost container of the window: Make sure the StatusBar is inside the bottommost container of the window, such as a Grid or StackPanel, and that it is placed in the last row of the container.
The container's vertical alignment is not set to "Bottom": Ensure that the container that holds the StatusBar has its VerticalAlignment property set to "Bottom".
The window's height is not being set correctly: Make sure that the window's height is being set correctly, and that it is large enough to accommodate the size of the StatusBar.
The layout of the window is not correct: Check that the layout of the window is correct and that all elements are positioned correctly. You can use the Snapping and Guides feature of the Visual Studio Designer to align elements correctly.
The window is using a template that does not include the StatusBar, if you are using a custom template for the window, check that it includes the StatusBar and that it is positioned correctly.
By troubleshooting these potential causes, you should be able to resolve the issue and position the StatusBar at the bottom of the window.

In my case the order of how I put the children into the Dockpanel had an influence. So, this was the correct order to fill proberly:
<Button DockPanel.Dock="Top" Height="50">Top</Button>
<Button DockPanel.Dock="Bottom" Height="50">Bottom</Button>
<Button DockPanel.Dock="Left" Width="50">Left</Button>
<Button DockPanel.Dock="Right" Width="50">Right</Button>

Related

Howto introduce a second menubar in WPF that stretches from top to bottom of screen

I want to introduce a second menubar that stretches from top to bottom of screen.
<StackPanel Orientation="Vertical" Background="Beige" VerticalAlignment="Stretch">
<ToggleButton Content="One Button in second menubar"/>
</StackPanel>
Seems as if the VerticalAlignment does not what I supposed it does. The StackPanel is minimized around the ToggleButton. How can I have it stretched from top to bottom of the screen?
Do you mean something like this?
<DockPanel>
<ToolBarTray DockPanel.Dock="Left" Orientation="Vertical" Background="Beige">
<ToolBar>
<ToggleButton Content="One Button in second menubar"/>
</ToolBar>
</ToolBarTray>
<Grid />
</DockPanel>
I tested your code by putting it in an empty window and it works just like you wanted it to. Make sure it doesn't get size restrictions from it's parent

ScrollViewer scrolls back when released

When I tap and hold and move my list around, it moves, but when I no longer hold, it smoothly scrolls back to beginning of the list. How do I prevent it?
I also tried putting whole ItemsControl inside ScrollViewer - same behavior.
Without ScrollViewer ItemsControl doesn't scroll at all.
I set breakpoints to ManipulationCompleted of ItemsPresenter, ItemsControl and ScrollViewer - in all of them scroll appears in correct position where I left it, but afterwards somewhere it scrolls back.
Here's my xaml markup:
<Grid x:Name="LayoutRoot">
<ItemsControl Name="lbDeployments"
ItemsSource="{Binding Path=Deployments}"
ItemTemplate="{StaticResource DeploymentItem}" >
<ItemsControl.Template>
<ControlTemplate TargetType="ItemsControl">
<Grid>
<ScrollViewer>
<ItemsPresenter />
</ScrollViewer>
</Grid>
</ControlTemplate>
</ItemsControl.Template>
</ItemsControl>
</Grid>
Please help
The issue is that the ScrollViewer needs a container of a fixed size to know much it can actually scroll.
You'll need to set the height of the ScrollViewer or the grid that contains it for scrollign to work properly.
Without doing this the scrollviewer thinks it can take as much space as it likes. When this is the case it doesn't need to scroll and what you see when manipulating the content directly isn't actually scrolling but just moving the content wihtin the scrollable area.
Try adding RowDefinitions:
<Grid x:Name="LayoutRoot">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
</Grid>
...
</Grid>

wpf layout help

I have the following xaml which resides in a wpf user control -
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="30" />
<RowDefinition Height="*" />
<RowDefinition Height="30" />
</Grid.RowDefinitions>
<TextBox
x:Name="MyTxt"
TextWrapping="WrapWithOverflow"
Grid.Row="0"
/>
<ListView
x:Name="MyList"
ItemsSource="{Binding}"
Grid.Row="1"
/>
<Label
Grid.Row="2"
/>
</Grid>
This control is nested within a grid in a view. I would like to have the text box be a set height at the top of the grid, the label at the bottom showing as a fixed height at the bottom of the grid. I want the list view to fill the rest of the screen area.
The problem that I am having is the listview does not size correctly. If I have too many records that show up in it, it extends beyond the window and no scroll bars are available to scroll down. I therefore cannot get to the bottom to see the vertical scroll bar if the data stretches off to the right of the screen.
I was able to set the listview to a fixed height and that worked, but I would like it to be more dynamic and resize with the window if possible.
Does anyone have any tips that might get the sizing correct?
Thanks for any thoughts.
EDIT - Here is the xaml for the containing grid in the mainwindow view. this was adapted from the article by Josh Smith here
<Grid>
<Border
Style="{StaticResource MainBorderStyle}"
>
<HeaderedContentControl
Content="{Binding Path=Workspaces}"
ContentTemplate="{StaticResource WorkspacesTemplate}"
/>
</Border>
</Grid>
I do have the scrollviewer properties set as mentioned in some of the answers below.
Here is the datatemplate for the workspace
<DataTemplate x:Key="WorkspacesTemplate">
<TabControl
IsSynchronizedWithCurrentItem="True"
ItemsSource="{Binding}"
ItemTemplate="{StaticResource ClosableTabItemTemplate}"
Margin="4"
/>
</DataTemplate>
Can you just add these properties to the listview?
ScrollViewer.CanContentScroll = "True"
ScrollViewer.VerticalScrollBarVisibility="Visible" <!-- or "Auto" -->
Everything else looks ok to me. You have the 3 rows, 2 of which are absolute, the other stretching. You also have the listview in the 2nd row, so it should stretch with it.
if that doesn't work, try wrapping the ListView in a scrollviewer
<ScrollViewer>
<ListView/>
</ScrollViewer>
What is the VerticalAlignment of a ListBox by default? You might need to set the vertical alignment to Stretch.
<ListView
x:Name="MyList"
ItemsSource="{Binding}"
Grid.Row="1"
VerticalAlignment="Stretch"
/>
I was able to get it working. If I change the containing grid in the main window to use a ContentControl instead of a HeaderedcontentControl, it works as expected.
Thank for any help.

Align items in a stack panel?

I was wondering if I can have 2 controls in a horizontal-oriented StackPanel so that the right item should be docked to the right side of the StackPanel.
I tried the following but it didn't work:
<StackPanel Orientation="Horizontal">
<TextBlock>Left</TextBlock>
<Button Width="30" HorizontalAlignment="Right">Right<Button>
</StackPanel>
In the snippet above I want the Button to be docked to the right side of the StackPanel.
Note: I need it to be done with StackPanel, not Grid etc.
You can achieve this with a DockPanel:
<DockPanel Width="300">
<TextBlock>Left</TextBlock>
<Button HorizontalAlignment="Right">Right</Button>
</DockPanel>
The difference is that a StackPanel will arrange child elements into single line (either vertical or horizontally) whereas a DockPanel defines an area where you can arrange child elements either horizontally or vertically, relative to each other (the Dock property changes the position of an element relative to other elements within the same container. Alignment properties, such as HorizontalAlignment, change the position of an element relative to its parent element).
Update
As pointed out in the comments you can also use the FlowDirection property of a StackPanel. See #D_Bester's answer.
Yo can set FlowDirection of Stack panel to RightToLeft, and then all items will be aligned to the right side.
For those who stumble upon this question, here's how to achieve this layout with a Grid:
<Grid>
<TextBlock Text="Server:"/>
<TextBlock Text="http://127.0.0.1" HorizontalAlignment="Right"/>
</Grid>
creates
Server: http://127.0.0.1
Could not get this working using a DockPanel quite the way I wanted and reversing the flow direction of a StackPanel is troublesome. Using a grid is not an option as items inside of it may be hidden at runtime and thus I do not know the total number of columns at design time. The best and simplest solution I could come up with is:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<StackPanel Grid.Column="1" Orientation="Horizontal">
<!-- Right aligned controls go here -->
</StackPanel>
</Grid>
This will result in controls inside of the StackPanel being aligned to the right side of the available space regardless of the number of controls - both at design and runtime. Yay! :)
This works perfectly for me. Just put the button first since you're starting on the right. If FlowDirection becomes a problem just add a StackPanel around it and specify FlowDirection="LeftToRight" for that portion. Or simply specify FlowDirection="LeftToRight" for the relevant control.
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right" FlowDirection="RightToLeft">
<Button Width="40" HorizontalAlignment="Right" Margin="3">Right</Button>
<TextBlock Margin="5">Left</TextBlock>
<StackPanel FlowDirection="LeftToRight">
<my:DatePicker Height="24" Name="DatePicker1" Width="113" xmlns:my="http://schemas.microsoft.com/wpf/2008/toolkit" />
</StackPanel>
<my:DatePicker FlowDirection="LeftToRight" Height="24" Name="DatePicker1" Width="113" xmlns:my="http://schemas.microsoft.com/wpf/2008/toolkit" />
</StackPanel>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<TextBlock Text="Left" />
<Button Width="30" Grid.Column="1" >Right</Button>
</Grid>
If you are having a problem like the one I had where labels were centered in my vertical stack panel, make sure you use full width controls. Delete the Width property, or put your button in a full-width container that allows internal alignment. WPF is all about using containers to control the layout.
<StackPanel Orientation="Vertical">
<TextBlock>Left</TextBlock>
<DockPanel>
<Button HorizontalAlignment="Right">Right</Button>
</DockPanel>
</StackPanel>
Vertical StackPanel with Left Label followed by Right Button
I hope this helps.
for windows 10
use relativePanel instead of stack panel, and use
relativepanel.alignrightwithpanel="true"
for the contained elements.
Maybe not what you want if you need to avoid hard-coding size values, but sometimes I use a "shim" (Separator) for this:
<Separator Width="42"></Separator>

Perfect Center on WPF Canvas

Since the canvas requires a Top/Left for placement, if you want to center something, is adding a grid at the proper Canvas.Top with HorizontalAlignment="Center" the best way to do it, or is there a better way?
This snip is a 150X300 canvas, with some content centered in a grid ....
<Canvas Width="150" Height="300">
<Grid Canvas.Top="75" Width="106" HorizontalAlignment="Center">
{whatever you want centered}
</Grid>
</Canvas>
Guy's solution works, but you may have to tweak z-order and visibility if you're juggling hit testing.
Another alternative is having the Grid inside the Canvas (as you've specified in your XAML) with the Height/Width set to (or bound to) the Height/Width of the Canvas. Then setting HorizontalAlignment/VerticalAlignment to Center for the contents of your Grid.
I'm not sure if this will meet your exact requirement, but if you put both the canvas and the content inside a grid as peers, it will get you a centered result:
<Grid>
<Canvas Width="150" Height="300"/>
<Button HorizontalAlignment="Center" VerticalAlignment="Center" Width="106" Content="Click"/>
</Grid>

Resources