Reputation: 22859
I am currently having an issue with a WPF Popup where an action on said popup wants to return the Focus to the Main window of the application.
Usually, when you click somewhere on the window the popup closes, which is the desired behaviour. However, I found no programmatic way to bring the focus back to the window from an action inside the popup in much the same way.
The following code outlines the problem:
Xaml:
<Window x:Class="PopupFocusEtc.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" x:Name="_wdw">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition /><ColumnDefinition />
</Grid.ColumnDefinitions>
<TextBox x:Name="_txt" Text="Test"
Grid.ColumnSpan="2" />
<Button Grid.Row="1" x:Name="_btnOpen"
Click="OpenPopup"
Content="Open Popup" Grid.ColumnSpan="2" />
<Popup x:Name="_popup"
Placement="Bottom"
HorizontalOffset="10"
PlacementTarget="{Binding ElementName=_btnOpen}"
StaysOpen="False">
<Button Click="SwitchFocus"
MinHeight="100" MinWidth="100"
Margin="5"
Content="Focus" />
</Popup>
<Rectangle Grid.Column="1" Grid.Row="2"
Fill="DarkOliveGreen">
<Rectangle.Style>
<Style TargetType="Rectangle">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Cursor" Value="Hand" />
</Trigger>
</Style.Triggers>
</Style>
</Rectangle.Style>
</Rectangle>
</Grid>
</Window>
code-behind:
using System.Windows;
using System.Windows.Input;
namespace PopupFocusEtc
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void OpenPopup(object sender, RoutedEventArgs e)
{
_popup.IsOpen = true;
}
private void SwitchFocus(object sender, RoutedEventArgs e)
{
_txt.Focus();
Keyboard.Focus(_txt);
_txt.SelectAll();
_wdw.Activate();
}
}
}
When I click the button in the popup what happens is that the textbox gets correctly focussed, I can even enter text, but the Popup stays open. Also, note the MouseOver effect on the rectangle that switches the cursor to a hand. Even though the window has focus, the effect is not working.
Only when I click into the window will the popup disappear, the effect works again, etc.
My question is: Given this situation, How do I get the main window activated again in such a way as if I would have clicked it?
UPDATE
I know that in the given example I can simply close the popup in the code-behind. This is a simplified example - In another app, the action started from within the Popup UI doesn't know that it lives on a Popup, and whatever happens on the main window doesn't know about the popup either.
Hence, before introducing more coupling in the UI, I would like to know if the mechanism that runs off when you click into the window can somehow be triggered programmatically...
Upvotes: 0
Views: 545
Reputation: 5785
I'm not clear on whether or not the example you provide above mimics the desired approach, but if you just add the line:
_popup.IsOpen = false;
in the SwitchFocus
method (I would put it before the _wdw.Activate()
, but it also works after), then the popup closes when you click the button, and the TextBox
receives focus.
Is this what you're looking for?
Upvotes: 1