新窗口在当前打开

时间:2009-11-06 19:49:00

标签: c# wpf window

我有一个窗口,我正在构建代码,并显示:

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 个答案:

答案 0 :(得分:1)

您是否尝试过设置窗口的TopMost属性?

您可以在MSDN

上找到更多信息
  

获取或设置一个值,该值指示窗口是否出现在最顶层的z顺序中。这是一个依赖属性。

正如您在评论中指出的那样,这将使窗口始终位于最顶层。在窗口显示后重置标志也是“hackish”。

修改

我刚刚在您的代码中看到您将窗口的Owner属性设置为:

wndViewer.Owner = (Window)dpParent;

我用过这个似乎“正常工作”的东西:

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

现在,在这种情况下AboutBox来自Window而不是UserControl,所以这里可能会有一些内容,但您是否需要将Owner设置为this以外的其他内容?