2013-05-11 20 views
0

在我的解決方案中,我有一個包含MainWindow(a.k.a.,我的應用程序的主窗口)的項目。在同一解決方案中的另一個項目中,我有一個聊天客戶端用戶控件,當用戶收到新消息時會顯示一個通知窗口。如何將我的通知窗口設置爲出現在MainWindow之上?

我希望我的通知窗口在MainWindow可能出現的任何位置上彈出。只要用戶有1個監視器,下面的代碼就可以工作,或者如果多個監視器,MainWindow最大化。但是,如果MainWindow最小化,通知將顯示在工作區域的右上角,而不是MainWindow。

我嘗試了下面的每個代碼集(在ChatClient.xaml.cs中執行),但都沒有成功。

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; 

如何讓我的通知窗口,總是在主窗口的頂部顯示?我應該在MainWindow.xaml.cs中執行它嗎?謝謝你的幫助!

編輯

下面的代碼還使我的應用程序(通過主窗口)崩潰。最好我可以告訴,這將窗口所有者設置爲用戶控件,而不是MainWindow。我需要做些什麼呢?

popNotification.Owner = Window.GetWindow(this); 

也試過這樣:

popNotification.Owner = Window.GetWindow(Application.Current.MainWindow); 

回答

1

您需要設置自定義窗口中打開CenterOwner,然後設置窗口所有者這樣

TestWindow window = new TestWindow(); 
window.Owner = Window.GetWindow(this); 
window.ShowDialog(); 
+0

我沒有我自己打開的「對話框」。我試圖在我的用戶控件中引用MainWindow類似於這篇文章:http://stackoverflow.com/questions/607370/wpf-how-do-i-set-the-owner-window-of-a-dialog-所示-通過-A-用戶控件;請參閱上面的編輯。 – 2013-05-12 15:56:03

0

我想通了做以下:

@ jugg1es答案相結合,使代碼看起來像:

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; 
} 

這不是完美的,但它是可行的,比以前好多了。

相關問題