I am having trouble setting the owner of Popup window to Application Main window.
Here what I did.
Created a Main Window with custom PopupWindow.
<Window x:Class="MainWindow" ... <Window.Resources> <Style x:Key="MessageWindowStyle" TargetType="{x:Type Window}"> <Setter Property="Background" Value="Transparent" /> <Setter Property="WindowStyle" Value="None" /> <Setter Property="ResizeMode" Value="NoResize" /> <Setter Property="BorderThickness" Value="1" /> <Setter Property="SnapsToDevicePixels" Value="True" /> <Setter Property="ShowInTaskbar" Value="False"/> <Setter Property="AllowsTransparency" Value="True"/> </Style> </Window.Resources> <i:Interaction.Triggers> <interactionRequest:InteractionRequestTrigger SourceObject="{Binding MessageRequest, Mode=OneWay}"> <interactionRequest:PopupWindowAction IsModal="True" CenterOverAssociatedObject="True" WindowStyle="{StaticResource MessageWindowStyle}" > <interactionRequest:PopupWindowAction.WindowContent> <controls:PageCustomPopup /> </interactionRequest:PopupWindowAction.WindowContent> </interactionRequest:PopupWindowAction> </interactionRequest:InteractionRequestTrigger> </i:Interaction.Triggers> ... </Window>
PageCustomPopup.xaml
This is basic xaml for the popup window.
<UserControl x:Class="PageCustomPopup"
...
</UserControl>
- PageCustomPopup.xaml.cs
In this page code-behind, I tried to set the parent, when the popup window is loaded. But it throws exception.
public partial class PageCustomPopup : UserControl, InteractionRequestAware, INotifyPropertyChanged
{
public PageCustomPopup()
{
Loaded += (sender, args) =>
{
try
{
var parentWindow = this.Parent as Window;
if (parentWindow != null)
{
// This line fails with "Cannot set the Owner after the window is displayed."
//parentWindow.Owner = Application.Current.MainWindow;
// First, default to the main window's dimensions
parentWindow.Width = Application.Current.MainWindow.Width;
parentWindow.Height = Application.Current.MainWindow.Height;
parentWindow.Left = Application.Current.MainWindow.Left;
parentWindow.Top = Application.Current.MainWindow.Top;
}
}
}
}
}
MainWindow code behind.
public InteractionRequest<Notification> MessageRequest { get; private set; } public MainWindow() { ... MessageRequest = new InteractionRequest<Notification>(); ... } void Button_OnClick(object sender, EventArgs e) { MessageRequest.Raise(new Notification(){ Title = "Title Text", Content = "Message to Display"}); }
When the Custom popup window is displayed, it shows correctly in the center of the main window.
But the issue is, when all the windows are minimized, if the Application icon on the taskbar is clicked, the Application main window is displayed, and inavtive. Cannot select anything.
The popup window is hidden and cannot bring to front.
The only way to bring the Popup window in front, is using "Alt+Tab" keys.
My question is, how to set the Custom popup window owner to Application.Current.MainWindow? So that, by selecting taskbar icon or Alt+Tab, displays both Mainwindow and popup window.