WPF: How to open User Control Popup on Fixed Position - wpf

I made a button wrapper around a UserControl, so that it operates as a button. When pressed, a popup screen is visible. However I want the popup screen to be a fixed location: For example upper right side of the UI.
Within this example the popup appears right on top of the UserControl.
<Popup IsOpen="{Binding IsChecked, ElementName=button}" x:Name="OpenPopup_FFU" StaysOpen="False" PlacementTarget="{Binding ElementName=button}">
<popup:Popup_FFU Height="500" Width="300"/>
</Popup>

Related

How can I close a popup in WPF from inside its user control

I have implemented a toggle button with a popup similar to what is described here. My popup displays a user control that is basically a custom menu with several images/buttons on it. Here is the code for my toggle button:
<ToggleButton x:Name="btnMenu" Background="Transparent" HorizontalAlignment="Right">
<StackPanel>
<Image Width="60" Height="60" Margin="10,0,10,0" Source="{StaticResource icon-menu}"/>
<Popup StaysOpen="False" IsOpen="{Binding IsChecked, ElementName=btnMenu}" Placement="Bottom">
<controls:Menu/>
</Popup>
</StackPanel>
</ToggleButton>
Everything works fine, except that when I click on a button (which is bound to a command) in my user control, I would like the popup to disappear. Is there a way, through xaml, that I can close the parent popup when a button on the user control is pressed?
I did manage to find solutions on codeproject which would most likely solve the problem, and I also found various solutions on StackOverflow where all the xaml was inside the popup instead of a separate user control. I wanted to see if there was another preferred method (hopefully xaml based) that could solve this particular problem, and still let me keep my menu in a separate user control.

WPF Popup PlacementTarget=0,0 of ToggleButton

I have a popup that appears when I click a togglebutton like so.
<ToggleButton HorizontalAlignment="Right" Template="{StaticResource MyToggleButton}" Name="MyToggleButton" />
<Popup Name="MyPopUp" IsOpen="{Binding ElementName=MyToggleButton, Path=IsChecked}" StaysOpen="False" PlacementTarget="{Binding ElementName=MyToggleButton}" Placement="Center">
// blah blah
</Popup>
How can I get the popup to appear at the 0,0 position of my toggle button? (The top left of my popup is the same position as the top left of the toggle button)
I have tried using PlacementTarget and can get it to appear left/right/top/botton but now exactly over the top of the ToggleButton
Easy: Set Placement to Relative, and leave Popup.VerticalOffset and Popup.HorizontalOffset both at their default value of 0.
<Popup
Name="MyPopUp"
IsOpen="{Binding ElementName=MyToggleButton, Path=IsChecked}"
StaysOpen="False"
PlacementTarget="{Binding ElementName=MyToggleButton}"
Placement="Relative"
>
Out of curiosity, are you doing this to prevent the user from clicking on the ToggleButton while the popup is open?

How to lock focus on popup in wpf

Is there any way in WPF to lock the focus on the popup so that we cannot set focus on other elements on the view?
I have a popup and have bound it to the IsOpen property of the ViewModel:
<Popup x:Name="myPopup"
Width="{Binding ElementName=RootLayout, Path=ActualWidth}"
Height="350"
PlacementTarget="{Binding ElementName=RootLayout}"
Placement="Center"
IsOpen="{Binding IsOpen}"
StaysOpen="True"
AllowsTransparency="True">
/// Popup elements
</Popup>
It works fine and when the IsOpen property in the ViewModel is set to true the popup shows up. but the problem is when I press the tab button, the focus goes to the other elements on the view while I need it to say on the popup.
How can I do that please? any help is appreciated.

How to open pop-up from link button and close it using button inside the pop-up?

I have main window, containing grid with different user controls. Each control is defined in separate Xaml. Then in MainWindow.xaml i match all particles of my main window.
One of my userControls is a menu, containing buttons.
i want to call popup after clicking some button in menu. Popup will contain some text, buttons and will overlay the main window.
So the question is how to call pop-up from menu-button and close it from button located on pop-up?
Thanks!
You can put a togglebutton in your menu, then bind the IsOpen property of the Popup to the IsChecked property of the togglebutton.
<ToggleButton x:Name="toggle" Content="ToggleButton" HorizontalAlignment="Left" Height="68" Margin="78,104,0,0" VerticalAlignment="Top" Width="102"/>
<Popup IsOpen="{Binding ElementName=toggle, Path=IsChecked}" StaysOpen="False">
<Grid Background="#FFE5E5E5" Height="299" Width="269">
<ToggleButton IsChecked="{Binding ElementName=toggle, Path=IsChecked}" Content="Close" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75"/>
</Grid>
</Popup>
Change StaysOpen back to True (the default) if you don't want the popup to go away when you click off it. In this situation, the togglebutton inside the popup will look a bit strange by default, but you can style it to look better.
If not all your code is in the same Xaml file, instead of binding by ElementName, you can bind them all to a boolean property in your viewmodel.

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.

Resources