0

In my solution, I have a project that contains MainWindow (a.k.a., the main window of my application). In another project in the same solution, I have a chat client user control that displays a notification window when the user receives a new message.

I want my notification window to pop-up on top of MainWindow wherever MainWindow might appear. The code below works as long as the user has 1 monitor OR MainWindow is maximized if multiple monitors. However, if MainWindow is minimized, the notification appears in the top right of the working area, not MainWindow.

I have tried each of the code sets below (executed in ChatClient.xaml.cs) with no success.

private const double topOffset = 0;
private const double leftOffset = 300;

popNotification.Top = Application.Current.MainWindow.Top + topOffset;
popNotification.Left = Application.Current.MainWindow.Width - leftOffset;

OR

popNotification.Top = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Top + topOffset;
popNotification.Left = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Right - leftOffset;

How do I get my notification window to always display on top of MainWindow? Should I execute it inside MainWindow.xaml.cs? Thanks for any help!

EDIT

The code below also causes my application (via MainWindow) to crash. Best I can tell, this sets the window owner to the user control, not MainWindow. What do I need to do instead?

popNotification.Owner = Window.GetWindow(this);

Also tried this:

popNotification.Owner = Window.GetWindow(Application.Current.MainWindow);
Accessory Freak
  • 75
  • 1
  • 4
  • 13

2 Answers2

1

You need to set the custom window to open CenterOwner and then set the window owner like this

TestWindow window = new TestWindow();
window.Owner = Window.GetWindow(this);
window.ShowDialog();
jugg1es
  • 1,560
  • 18
  • 33
  • I don't have a "dialog" that I'm opening per se. I'm attempting to reference MainWindow in my user control similar to this post: http://stackoverflow.com/questions/607370/wpf-how-do-i-set-the-owner-window-of-a-dialog-shown-by-a-usercontrol; see edits above. – Accessory Freak May 12 '13 at 15:56
0

I figured it out doing the following:

So the code looks like:

public partial class ChatControl : User Control
{
    PopNotifications popNotification = new PopNotifications();
    --and other code
}

public ChatControl()
{
    InitializeComponent();
    this.Loaded += new RoutedEventHandler(ChatControl _Loaded);
}

private void ChatControl _Loaded(object sender, RoutedEventArgs e)
{
    Window parentWindow = Window.GetWindow(this);

    popNotification.Owner = parentWindow;
    popNotification.Top = popNotification .Owner.Top;
    popNotification.Left = popNotification .Owner.Left;
    popNotification.WindowStartupLocation = WindowStartupLocation.Manual;
}

It's not perfect but it's workable and much better than before.

Community
  • 1
  • 1
Accessory Freak
  • 75
  • 1
  • 4
  • 13