Align elements horizontally to max width but wrap when parent squeezed - wpf

I have a Grid which contains two Buttons. I want the first button to align to left and the second one to the right (this is why I'm using Grid as the parent). When the whole window is squeezed so much that the buttons cannot be entirely visible, I want them to wrap - ie. first one stays aligned to left but the second one is moved to a new row (and preferably but not necessarily is still aligned to right). For that I'd use a WrapPanel but the WrapPanel will not fill the parent window and hence will not align the buttons to left and right.
Is there any way to get both - the horizontal alignment and wrapping?
My current code:
<Grid Grid.Row="2" Visibility="{Binding Attachments.Count, Converter={StaticResource GreaterThanGivenValueVisibilityConverter}, ConverterParameter=1}">
<Button Grid.Row="1" Grid.Column="1" HorizontalAlignment="Left" VerticalAlignment="Top" Height="30" BorderThickness="0" Focusable="False" Command="{Binding ExpandAttachmentsPanel}">
</Button>
<Button Grid.Row="1" Grid.Column="2" HorizontalAlignment="Right" VerticalAlignment="Top" Height="30" BorderThickness="0" Focusable="False" Command="{Binding DownloadAllAttachmentsCommand}">
</Button>
</Grid>

Is there any way to get both - the horizontal alignment and wrapping?
There is no built-in Panel that gives you this behaviour but you should be able to handle the SizeChanged event of the WrapPanel and programmatically adjust the Margin of one of the Buttons. Something like this:
<WrapPanel x:Name="wp" Background="Yellow" SizeChanged="WrapPanel_SizeChanged">
<Button x:Name="btn1" Width="100">
<TextBlock>1</TextBlock>
</Button>
<Button x:Name="btn2" Width="100">
<TextBlock>2</TextBlock>
</Button>
</WrapPanel>
private void WrapPanel_SizeChanged(object sender, SizeChangedEventArgs e)
{
btn1.Margin = new Thickness(0, 0, Math.Max(0, wp.ActualWidth - btn1.ActualWidth - btn2.ActualWidth), 0);
}

Related

How to resize popup in right direction?

I have a popup for which the size changes dynamically to fit the content. When the size increases, popup width increased in left direction only until it reaches left edge of the window. But in my case, I want popup width to be increased in right direction.
Below is the code snippet I am using
XAML:
<StackPanel>
<TextBox x:Name="popupText" Margin="5"/>
<Button Content="Change PopupText" Click="Button_Click_1" Margin="5"/>
<Canvas x:Name="canvas" Width="200" Height="200" Background="Red">
<Rectangle x:Name="rect" Canvas.Top="50" Canvas.Left="50"
Width="50" Height="100"
Stroke="White" StrokeThickness="3"/>
<Popup x:Name="popup" AllowsTransparency="true"
IsOpen="True" Placement="Relative"
PlacementTarget="{Binding ElementName=canvas}"
PlacementRectangle="50,50,50,100"
HorizontalOffset="0"
>
<TextBlock x:Name="textBlock" FontSize="14" Background="Yellow"
TextWrapping="Wrap" >
This is a popup with a PlacementRectangle.
</TextBlock>
</Popup>
</Canvas>
</StackPanel>
C#:
private void Button_Click_1(object sender, RoutedEventArgs e)
{
this.textBlock.Text += this.popupText.Text;
}
ScreenShots:
Before changing size
After changing size
Type anything in the textbox and click "Change PopupText" button. you can clearly see popup size increasing in left direction.
Is above one behavior of popup? Could I change this behavior and increase popup width in right direction?
Thanks in Advance,
Shobika.
You may want to have a look at custom popup placements.
Set Placement="Custom" on your popup and use the CustomPopupPlacementCallback property to reference a callback. In the callback, you can calculate preferred placements, relative to your placement target.

WPF Dockpanel won't align right

I have this code on which I use a DockPanel on a Button.Content. However it doesn't let me right align the last image (small arrow).
<Button Height="70"
HorizontalContentAlignment="Left">
<Button.Content>
<DockPanel LastChildFill="False">
<Image DockPanel.Dock="Left"
Height="50"
Width="50"
Source="{StaticResource Placeholder}"
Stretch="UniformToFill"
Margin="5" />
<TextBlock DockPanel.Dock="Left"
Text="Dummy text"
VerticalAlignment="Center"
Margin="5" />
<Image DockPanel.Dock="Right"
Height="24"
Width="24"
Source="{StaticResource Right_Arrow_24}"
VerticalAlignment="Center"
HorizontalAlignment="Right"
Stretch="UniformToFill"
Margin="5" />
</DockPanel>
</Button.Content>
</Button>
It now gives me this:
So the right small arrow should be placed on the right of the button and not just after the TextBlock.
I've found some similar issues and it looks like I did it correct, but somehow it isn't..
What am I doing wrong here?
Try to set the HorizontalContentAlignment of your button to "Stretch". Otherwise your DockPanel will use the size need by its content, then align left. You can confirm this behaviour by using different text lengths for your TextBlocks
You simply have to append an extra child to the DockPanel (say an empty Canvas) without the DockPanel.Dock attribute, so that all the remaining space is assigned to it.
In general, DockPanel only works fine only when its last child has no Dock constraint
Try setting
HorizontalContentAlignment="Stretch"
On your button.

VerticalAlignment="Stretch" for label inside canvas doesn't work

How to use VerticalAlignment="Stretch" with a Label inside a Canvas? I'm trying to center the text "Cancel" in the button as in the code below. To use fixed height and width for the label isn't a desired option.
<Button Name="buttonCancel" Width="80" Height="40" IsCancel="True" Padding="0" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch">
<Canvas>
<Label Canvas.Top="0" Canvas.Left="0" Padding="0" FontSize="10">Esc</Label>
<Label VerticalContentAlignment="Center" HorizontalContentAlignment="Center" VerticalAlignment="Stretch" HorizontalAlignment="Stretch">Cancel</Label>
</Canvas>
</Button>
Use a binding to the Canvas's ActualWidth:
<Canvas>
<Label Width="{Binding RelativeSource={RelativeSource AncestorType={x:Type Canvas}}, Path=ActualWidth}">...</Label>
</Canvas>
But as mentioned above, if you are interested in dynamic stretching layouts, the Canvas is not the ideal choice of control.
A Canvas does not perform any scaling layout of its contents; if you want to scale the contents, you could use Grid in this case, which will, by default, scale both Label elements to fill the Content space.
Assuming you need the canvas for other objects that are of a fixed nature, you could overlay the Canvas on a Grid, and then put the labels in the grid. You can put the labels before the canvas to make them background z-index (overwritten by canvas objects) or after the canvas to make them higher z-index (will overwrite canvas objects). For example:
<Button Name="buttonCancel" Width="80" Height="40" IsCancel="True" Padding="0" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch">
<Grid>
<Label Padding="0" FontSize="10">Esc</Label>
<Label VerticalContentAlignment="Center" HorizontalContentAlignment="Center" VerticalAlignment="Stretch" HorizontalAlignment="Stretch">Cancel</Label>
<Canvas>
<!-- Your Canvas content here -->
</Canvas>
</Grid>
</Button>
Repeating my solution from the comments, since (a) you really don't want a Canvas and (b) it sounds like this solved your problems, so I'll make it an answer where it will be more visible to others.
Canvas is meant for fixed-pixel-size layouts, which is probably the least common case. You should replace your Canvas with a Grid as shown below, so that both Labels are laid out dynamically (and independently) within the available space:
<Grid>
<Label Padding="0" FontSize="10">Esc</Label>
<Label VerticalAlignment="Center" HorizontalAlignment="Center">Cancel</Label>
</Grid>

Why ZIndex is not considered in arrange override of a stackpanel

Here is what I tried to to:
Added a stackpanel to my window (Orientation: Horizontal)
Added a set of buttons to it
Set the first button's ZIndex to be higher than the second one
Increased the width of the first button
What I expected:
I expected the first button to be on top of the second button (atlest overlay)
StackPanel's width should not change unless the width of the first button is no more sufficient
What is happening actually:
First button's width increases and the second button moves towards the right accordingly. They stay on the same plane
StackPanel's width increases with increase in the first button's width
Here is my question:
I know that stackpanel has not considered ZIndex while arranging the items within itself, BUT WHY?? Shouldn't it consider the ZIndex of its children while arranging them???
The Stackpanel 'stacks' its children based on their widths, i.e. if you increase the width of an item (or increase its margin), the stackpanel will simply expand to accomodate this. If you want to force items within a stackpanel to overlap, you will have to change their location after the layout has been computed. You can perform this using a RenderTransform. See the example below:
<StackPanel Orientation="Horizontal" VerticalAlignment="Top">
<Button Content="One" Canvas.ZIndex="10">
<Button.RenderTransform>
<TranslateTransform X="10"/>
</Button.RenderTransform>
</Button>
<Button Content="One"/>
<Button Content="One"/>
<Button Content="One"/>
</StackPanel>
And yes, the ZIndex is respected. This is an attached proepry of Canvas, however, it seems to be used by the rendering engine directly rather than by Canvas, hence it works in the above code.
I tried to find some relevant info about how to set the z index of wpf layout elements and panels. Using a Canvas comes with a different set of positioning issues which I simply hadn't the time to investigate. Here is a simple solution using the Panel.ZIndex property in xaml.
<Grid>
<Border Width="100" Height="100" Margin="0,0,50,50" Panel.ZIndex="1" Background="Navy" Opacity="0.3"
VerticalAlignment="Top" HorizontalAlignment="Left">
</Border>
<Border Width="100" Height="100" Margin="50,50,0,0" Background="Fuchsia" Opacity="0.3">
</Border>
The resulting two square border elements will overlap. One can use stackpanels instead of borders and use this logic to overlap anything easily.
Here is the same code adapted to the button problem:
<Grid>
<StackPanel Panel.ZIndex="10" Margin="20,20,0,0" HorizontalAlignment="Left">
<Button Content="One" Width="50" Height="40">
</Button>
</StackPanel>
<StackPanel Orientation="Horizontal" Margin="50,0,0,0" >
<Button Content="Two" Width="50" Height="40"/>
<Button Content="Three" Width="50" Height="40"/>
<Button Content="Four" Width="50" Height="40"/>
</StackPanel>
</Grid>

how to extend WPF GridRow Contents into next row without expanding the current row

I'm new to WPF and I'm trying to build a dropdown menu using the expander. Page layout is being handled with a Grid.
The extender sits inside the first row of the grid and I would I would like the contents of the expander to expand over top of the contents of everything below when it's clicked. Unfortunately, right now, the entire row is expanded to accommodate the height of the expanded control.
I tried playing around with the ZIndex of the Expander but it doesn't seem to have any effect. No matter what, the row always expands forcing everything else on the page to move.
<Expander FontSize="18" Name="moduleSelect" Width="100" Header=" Goto -> "
Background="#000033" MouseEnter="moduleSelect_MouseEnter"
MouseLeave="moduleSelect_MouseLeave" Foreground="White"
Grid.Column="0" Grid.ColumnSpan="3" Grid.Row="1" HorizontalAlignment="Left">
<StackPanel>
<Button Name="btnTasks" Width="100" Foreground="White"
Background="#000033">Tasks</Button>
<Button Name="btnNotes" Width="100" Foreground="White"
Background="#000033">Notes</Button>
</StackPanel>
</Expander>
How can I make this expand 'above' the subsequent rows without distorting the grid?
What would happen if you set the Grid.RowSpan of the Expander to 2 (or how ever many rows you'd like it to span when expanded)?
So, for a two-row grid, you'd have something like this:
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="30" /> <!--set this to the height of the expander's header area-->
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<WhateverGoesInRow2 Grid.Row="1" />
<Expander FontSize="18" Name="moduleSelect" Width="100" Header=" Goto -> "
Background="#000033" MouseEnter="moduleSelect_MouseEnter"
MouseLeave="moduleSelect_MouseLeave" Foreground="White"
Grid.Column="0" Grid.ColumnSpan="3" HorizontalAlignment="Left"
Grid.Row=0 Grid.RowSpan="2">
<StackPanel>
<Button Name="btnTasks" Width="100" Foreground="White" Background="#000033">Tasks</Button>
<Button Name="btnNotes" Width="100" Foreground="White" Background="#000033">Notes</Button>
</StackPanel>
</Expander>
</Grid>
You may need to adjust your RowDefinition section for your particular situation, but if I'm understanding your problem correctly, I think this will work.
You want something that pops up over the grid, not expands within the grid. A ComboBox, say, or - this being a menu, after all - a ContextMenu.
You could also build some combination of a ToggleButton and a Popup, but that's essentially the same thing as a ComboBox with IsEditable turned off.
The built-in drop-down control makes use of a Popup control in its default control template to do a similar thing.

Resources