2009-11-06 105 views
0

我有我在構建代碼的窗口,並顯示:新窗口中打開在當前

Window wndViewer = new Window(); 
wndViewer.Background = new SolidColorBrush(System.Windows.Media.Color.FromArgb(0xFF, 0x31, 0x31, 0x31)); 
wndViewer.WindowState = WindowState.Maximized; 
wndViewer.Width = 1024; 
wndViewer.Height = 768; 

Grid grd = new Grid(); 

wndViewer.Title = "<Removed>"; 

Viewer vw = new Viewer(); // This is a UserControl 
vw.StudyDate = ((StudyItem)sender).StudyDate.ToString("MM/dd/yyyy"); 
vw.PatientName = ((StudyItem)sender).PatientName; 
vw.PatientId = ((StudyItem)sender).OwnerName; 

vw.Margin = new Thickness(3, 30, 3, 3); 
vw.StudyInstance = ((StudyItem)sender).ItemStudy; 
grd.Children.Add(vw); 

wndViewer.Content = grd; 

refreshTimer.Stop(); 

wndViewer.Tag = vw.StudyInstance; 

wndList.Add(wndViewer); // List<Window> of all the windows opened this way. 

DependencyObject dpParent = LogicalTreeHelper.GetParent(this); 

while (dpParent != null && dpParent.GetType() != typeof(Window)) 
{ 
    dpParent = LogicalTreeHelper.GetParent(dpParent); 
} 

wndViewer.Owner = (Window)dpParent; 

wndViewer.ShowActivated = true; 

wndViewer.Show(); 

的問題是,我需要這個窗口中顯示當前窗口的頂部,它總是在當前窗口下出現。我已經嘗試了幾種解決方案:

wndViewer.BringIntoView(); 

導入和調用:

[DllImport("User32.dll")] 
public static extern Int32 SetForegroundWindow(int hWnd); 

[DllImport("user32.dll")] 
public static extern int FindWindow(string lpClassName, string lpWindowName);      

所以,我敢肯定,我在這裏忽視的東西。謝謝你的幫助!

〜〜的md5sum

回答

1

您是否嘗試設置窗口TopMost財產?

您可以找到MSDN

更多信息,獲取或設置一個值,該值指示是否出現在最頂層的z順序的窗口。這是一個依賴屬性。

正如你在你的評論中指出的那樣,這將使窗口始終是最重要的。窗口顯示後重置標誌也是「hackish」。

編輯

我剛看到你的代碼,你設置窗口的Owner屬性爲:

wndViewer.Owner = (Window)dpParent; 

我用這個,這似乎「只工作「:

var about = new AboutBox(); 
about.Owner = this; 
about.Initialise(); 
about.Show(); 

現在,在這種情況下AboutBoxWindow而衍生塔n UserControl,所以這裏可能有些東西,但是有什麼理由需要將Owner設置爲this以外的東西嗎?

+0

這似乎已經做了伎倆......但是,它有點「駭人聽聞」,因爲現在動態窗口必須從主窗口移出才能返回到主窗口。儘管現在它肯定會工作! – 2009-11-06 20:06:56

+0

在這種情況下,「this」不是一個窗口,而是一個UserControl,所以我得到了邏輯父窗口並使用它,因爲owner屬性只接受一個窗口。此外,Window類沒有可公開訪問的Initialise方法,因此必須特定於AboutBox。 – 2009-11-06 20:51:05

+0

(hackish或不)重置vw.Loaded事件處理程序中的標誌具有我在此需要的確切效果。窗戶出現在前面,但並不是始終強迫自己前行。 – 2009-11-06 20:57:21