How to lock focus on popup in wpf - 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.

Related

WPF: How to open User Control Popup on Fixed Position

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>

WPF Popup placement relative to main window

I have a WPF application that uses one window as main window. Now I want to display various popups centered in the window. This works fine when the popup is placed inside the window. However, I have implemented the popups as user control; meaning that the main window contains a user controls which itself contains the actual popup. So the UI element tree looks like this:
Window --> UserControlPopup --> Popup
The popup inside the user control is declared like this:
<UserControl x:Class="X.Custom_Controls.ErrorPopup"
...
xmlns:local="clr-namespace:X.Custom_Controls"
mc:Ignorable="d" d:DesignHeight="360" d:DesignWidth="500">
<Popup Name="errorPopup" Height="360" Width="500" Placement="Center" StaysOpen="True" PlacementTarget="{Binding ElementName=mainWindow}">
...
</Popup> </UserControl>
The mainWindow element name is the one of my main window, which is declared as follows:
<Window x:Class="X.MainWindow" x:Name="mainWindow" ...>
The problem is that the popup is not placed in the center, but on the left side. So my guess is that the popup cannot resolve the elementName properly (since it's a child of a child of the mainWindow). Does anyone know how I can solve this issue in XAML?
Update: the solution was to use
PlacementTarget="{Binding RelativeSource={RelativeSource AncestorType=Window}}"
Try to acces your main window through x:Reference. This one should work:
<MainWindow x:Name="mainWindow">
<UserControl Tag="{x:Reference mainWindow}">
</MainWindow>
<UserControl x:Name="userControl">
<PopUp PlacementTarget="{Binding Path=Tag, ElementName=userControl}"/>
</UserControl>
Such a way you can use your UserControl in another Controls/UserControls and you will not need to modify it, just set the Tag property from outside, if you use {RelativeSource AncestorType=Window} you will need to modify it.
The solution was to use the following as PlacementTarget:
PlacementTarget="{Binding RelativeSource={RelativeSource AncestorType=Window}}"

How to close the pop up window when I click outside of popup window

Here is XAML code for open popup window IsChecked btnViewDetail, I need to close popup on click out side of popup window.
<Popup IsOpen="{Binding IsChecked, ElementName=btnViewDetail}" PopupAnimation="Fade" Width="300" Height="225" PlacementTarget="{Binding ElementName=svTotalStock}" Placement="Top" StaysOpen="False">
<Grid Background="Black">
<TextBlock TextWrapping="Wrap" Text="Raw Materials details"
VerticalAlignment="Top" Height="25" FontFamily="Segoe UI Semibold"
Padding="7,6,0,0" FontWeight="Bold" FontSize="14" Foreground="White"
Margin="0,2,59,0"/>
<Border BorderThickness="1" BorderBrush="Black"/>
</Grid>
</Popup>
<Grid>
<Grid.ContextMenu>
<ContextMenu>
<MenuItem IsCheckable="True" Name="btnViewDetail" Header="View Details"/>
</ContextMenu>
</Grid.ContextMenu>
</Grid>
property of Popup StaysOpen = false do this work.
if StaysOpen property cannot handle your situation, you have to capture MouseDown event on your Window when your container element(in your situation, Grid) has Focusable="True"
private void Window_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
gridContainer.Focus();
}
There are two properties you have to assign:
StaysOpen = false ,
When the StaysOpen property is set to true, Popup stays open until it is explicitly closed by setting the IsOpen property to false. When StaysOpen is false, the Popup control intercepts all mouse and keyboard events to determine when one of these events occurs outside the Popup control.
Next, set popup's child : Focusable = false
Then in other User Control which u wants tp open the popup, define an EventTrigger on UIElement.LostMouseCapture to set the popup's IsOpen = true;
I suspect the control from where the popup is opened. I even had same issue and reloved by opening the popup by MouseLeftButtonUp of the target.
add a togglebutton and bind IsOpen property of popup to IsChecked property of ToggleButton as shown below:
<ToggleButton IsChecked = {Binding ElementName = "Your_Popup_Name", Path = "IsOpen", Mode = "TwoWay"} Opacity = "0" , Panel.ZIndex = "1">

WPF Datagrid Popup Placement

I am showing a Popup when users double click a DataGrid row, is there a way to show this popup at the right of the SelectedItem?
Here is my current code:
<Popup PlacementTarget="{Binding ElementName=dgWarnings}" Placement="Right" Name="InfoPopup" PopupAnimation="Slide" AllowsTransparency="True" IsOpen="{Binding ShowInfo}">
I obviously cannot bind to the selected item property as this is not a UI item.
As I mentioned I would like to display it on the right hand side of the currently selected item in the datagrid.
Any suggestions?

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.

Resources