Popup Control VerticalAlignment doesn't "catch" - wpf

I'm using a Popup control, and for some reason its vertical alignment doesn't work as I expect. This is the XAML I have:
<Grid Width="100" Height="30" >
<TextBox Text="Hello" />
<Popup IsOpen="True" VerticalAlignment="Bottom" HorizontalAlignment="Stretch" >
<Button Content="Hello" />
</Popup>
</Grid>
I expected that the Popup's Width would be 100px, the same as it's parent Grid.
However, the Button inside the popup behaves as if its HorizontalAlignment is Left, i.e., the Button's width is just enough to allow the word "Hello" inside of it, and I can't figure out why, or how to make it have the same width as the containing Grid.
Thanks!

Unfortunately, a Popup is not part of the same visual tree as the rest of the elements in the XAML in which it is defined. It is literally a new window, albeit a very simple and typically small window.
Put another way, the Popup doesn't really participate in the layout of the main window. So changing things like HorizontalAlignment has no effect because that property controls how layout positions and sizes this element relative to its containing element.
This "orphaned element" issue causes no end of problems when dealing with a Popup. Nevertheless, there are tools and techniques to address all the problems that being a separate window introduces.
The first suggestion I can give is to understand the placement properties, PlacementTarget and Placement. You can use these properties for positioning the Popup. You can also use the ElementName syntax of databinding to take care of sizing.
Here is an example of using these techniques in your situation:
<Grid Name="grid" Width="100" Height="30" >
<TextBox Text="Hello" />
<Popup IsOpen="True"
Placement="Bottom"
PlacementTarget="{Binding ElementName=grid}"
Width="{Binding ActualWidth, ElementName=grid}">
<Button Content="Hello" />
</Popup>
</Grid>

Related

How to avoid sizing grip overlap in Window

I made a WPF Window containing StatusBar with StatusBarItem and ProgressBar.
The Window has a property ResizeMode set to CanResizeWithGrip.
It shows the sizing grip properly, but it overlaps elements underneath:
How can I avoid this overlap? I can set right margin to the progress bar, but how large? I don't want to use any magic numbers or hardcoded values. It would be also nice to have this resolved purely in XAML.
You can change style for "Window" considering all wishes.
http://msdn.microsoft.com/en-us/library/aa969824(v=vs.110).aspx
or https://stackoverflow.com/a/8278917/3492412
Or can do something like this
<StatusBar VerticalAlignment="Bottom">
<StatusBarItem x:Name="statusbar" Background="Gray" HorizontalContentAlignment="Stretch">
<DockPanel>
<ResizeGrip DockPanel.Dock="Right" Visibility="Hidden" />
<ProgressBar Background="red" Height="20" Value="50" />
</DockPanel>
</StatusBarItem>
</StatusBar>

WPF Popup IsOpen issue

Using a concept found here on StackOverflow. Note that the ToggleButton.IsHitTestVisible is bound to Popup.IsOpen, with StaysOpen="False". This should mean that touching anywhere outside the Popup would cause it to close. However...
Touching/Clicking on an ListBoxItem in the ItemsControl won't close the Popup, as is intended. Touching anywhere else within the Popup does close it. That doesn't seem to add up, according to how this is set up.
<Grid ClipToBounds="True">
<Border Name="Root">
<ToggleButton x:Name="PART_Toggle"
ClickMode="Release"
IsHitTestVisible="{Binding ElementName=PART_Popup,
Path=IsOpen,
Mode=OneWay,
Converter={StaticResource BooleanInverter}}"/>
</Border>
<Popup x:Name="PART_Popup"
IsOpen="{Binding ElementName=PART_Toggle,
Path=IsChecked}"
PlacementTarget="{Binding ElementName=PART_Toggle}"
StaysOpen="False">
<Grid Background="Transparent">
<Grid>
<!-- Anything here (outside of the Item) -->
<ItemsControl>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
<!-- Anything in this item template works. The popup does not close -->
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
</Border>
</Grid>
</Popup>
</Grid>
Any ideas? Thanks.
Edit: Solved
Turns out this was happening because it was inside a custom control which was derived from ListBox. It didn't seem relevant at the time I made this question, sorry.
I think in your case the problem is either the position or size of the popup. When trying your code it did work, however I had to set Placement="Center" on the Popup and set the size of the grid inside the popup.
Without the former, the popup was not placed inside the while without the latter the popup's size was just that of its content (meaning there was no outside to click).
Try first setting the Popup's background to Red or something to see if the popup is actually positioned and sized correctly.
Turns out this was happening because it was inside a custom control which was derived from ListBox. It didn't seem relevant at the time I made this question, sorry.

PopUp in Silverlight is not being clipped by container control

Simple, yet frustrating issue here...
I have a PopUp control.
It is contained in side a Grid, which has a Grid.Clip defined.
The PopUp is still visible outside the Grid's clipped area.
i.e.
<Grid Background="Red" Width="150" Height="150">
<Grid.Clip>
<RectangleGeometry Rect="0,0,150,150" />
</Grid.Clip>
<Popup IsOpen="True" Margin="100,100,0,0">
<Grid Background="Green" Width="150" Height="150" />
</Popup>
</Grid>
Ideally, the green box should not appear or "bleed" outside of the red box. The problem is that it is contained within a PopUp, and so it bleeds. How can I modify this (without removing the PopUp control) so that the PopUp does not bleed outside of it's containing control?
Popup works differently. It "ignores" its parent and it is added directly into visual root of your app. This is how it can be on-top of everything.
So now it depends on what are you trying to do. I think popup is not suitable for this scenario.
You can try to clip the popup in its template, but I feel that's not what you want.

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>

Why is my Popup showing opposite the Placement property on some machines?

I have a simple WPF Popup that I am showing when the user clicks a Button.
<Button
x:Name="aButton"
Content="Up/Down"
Width="75"
Height="30"
Click="aButton_Click"
/>
<Popup
PlacementTarget="{Binding ElementName=aButton}"
Placement="Right"
VerticalOffset="-31"
StaysOpen="False"
AllowsTransparency="True"
>
<StackPanel>
<Button Width="45" Height="45" Margin="2,0,2,2" Content="+"/>
<Button Width="45" Height="45" Margin="2,0,2,0" Content="-"/>
</StackPanel>
</Popup>
What is extremely weird ... is that this code works differently depending on what machine it runs on.
I run this code on my main desktop and everything works just fine ... and as it should. I run it on my PDC09 netbook ... and the Popup shows opposite (on the left instead of the right as I told it to with the Placement property).
Why is this? And what can I do about it?
I couldn't find anything via Google ... but a lucky search in the WPF forum, quickly found this post. Note to self: don't forget to search the WPF forums if Google can't find anything.
The answer is that my PDC09 netbook is a Tablet PC at heart, and apparently, Microsoft thought it was a good idea to show the Popup opposite to the Placement property on a Tablet PC that is configured for right-handed people ... such that the Popup doesn't appear under the user's hand.
The solution is to revert to custom Popup placement ... if you don't want this behavior.
I would love to hear about any other ways around this problem.
I fixed this issue by adding a border in the same grid col/row as the desired placement target. Then set this as the placement target instead. By binding the width of this border to the popup content it will adjust it's width automatically therefore the alignment (left or right) is irrelevant. If you want to still control alignment, you can do that by aligning the placement target border.
Hope that makes sense, if not, here is a quick example.
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Popup x:Name="StartMenuPopup" Placement="Top" PlacementTarget="{Binding ElementName=PopupTarget}" >
<Border x:Name="PopupBorder">
</Border>
</Popup>
<Border x:Name="PopupTarget" Grid.Row="1" Width="{Binding ActualWidth, Mode=OneWay, ElementName=PopupBorder}"
BorderThickness="0" HorizontalAlignment="Left" VerticalAlignment="Top"/>
<startmenu:TaskBar Grid.Row="1">
<startmenu:TaskBar.StartButton>
<startmenu:ToggleMenu Width="36" x:Name="StartButton"
ImageData="{Binding StartButtonImageData}"
AssociatedPopup="{Binding ElementName=StartMenuPopup}"
IsOpen="{Binding StartMenuOpen, Mode=TwoWay}"/>
</startmenu:TaskBar.StartButton>
</startmenu:TaskBar>
</Grid>
The popup PlacementTarget binds to the PopupTarget border, and the PopupTarget border width binds back to the PopupBorder element. This makes the PopupTarget border the same width as the popup therefore negating the alignment issue.

Resources