I have a window that I'm building in code, and showing:
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();
The problem is that I need this window to be displayed on top of the curre开发者_StackOverflownt window, and it always comes up under the current window. I've tried several solutions:
wndViewer.BringIntoView();
Importing and calling:
[DllImport("User32.dll")]
public static extern Int32 SetForegroundWindow(int hWnd);
[DllImport("user32.dll")]
public static extern int FindWindow(string lpClassName, string lpWindowName);
So, I'm sure I'm overlooking something here. Thanks for any help!
~md5sum~
Have you tried setting the TopMost
property of the window?
You can find more information on the MSDN
Gets or sets a value that indicates whether a window appears in the topmost z-order. This is a dependency property.
As you point out in your comment, this will make the window always the top most. It would also be "hackish" to reset the flag after the window has been shown.
EDIT
I've just seen in your code that you're setting the Owner
property of the window to be:
wndViewer.Owner = (Window)dpParent;
I've used this which seems to "just work":
var about = new AboutBox();
about.Owner = this;
about.Initialise();
about.Show();
Now in this case AboutBox
is derived from Window
rather than UserControl
, so there may be something here, but is there any reason you need to set Owner
to something other than this
?
精彩评论