The title pretty much says it all, but basically I have a main parent window, which occasionally opens child windows. Right now it's possible to select one of the child windows from the Windows 7 taskbar, and only that window will be brought to the front. What I'd like it to find a way to link the parent window to this command, so that any time a child window is selected the parent is automatically brought to the front as well.
I tried to use both the Focus() and Topmost = true commands from within the child windows 'GotFocusevent handler, but neither seemed to make a difference. I also tried theBringIntoView()` method, but again, no joy. Has anyone seen this before or know a way to implement this?
This is what I've tried so far. The logic in setting mainWindow first and then immediately setting the child window is that I do still want the child window to have focus, but I want mainWindow to be above any other programs running (ie-Excel, VS, etc).
private void Window_GotFocus(object sender, RoutedEventArgs e)
{
var mainWindow = Application.Current.Windows.Cast<Window>().FirstOrDefault(window => window is MainWindow) as MainWindow;
mainWindow.Topmost = true;
this.Topmost = true;
}
I think you're listening to the wrong event to be notified when your window is selected. Subscribe to the Activated event on your child window which should let you know when your window is selected in the taskbar. From there you can Activate() your MainWindow.
Additionally, I think if you set the parent window to be the Owner of the child window you'll get this behavior automatically.
Related
I'm working in Silverlight, but potentially a WPF solution would work as well.
My problem is very simple. I have lots of modal Child Windows that can be open, and in their generic menu is a home button. This button is supposed to close all of the child windows and return to the base screen. I have a few different types of 'generic child windows' that host lots of different UserControls, so by far the easiest way to implement this is to, when the window comes into focus, check if the global ReturnToHome bool is true, and if it is, just close it.
I've tried all of these
private void ChildWindow_GotFocus(object sender, RoutedEventArgs e)
{
if (CommonResources.ReturnToHome) DialogResult = false;
}
private void ChildWindow_MouseEnter(object sender, MouseEventArgs e)
{
if (CommonResources.ReturnToHome) DialogResult = false;
}
private void ChildWindow_IsEnabledChanged(object sender, DependencyPropertyChangedEventArgs e)
{
if (CommonResources.ReturnToHome) DialogResult = false;
}
The issue is, GotFocus doesn't fire until I actually click on the window. MouseEnter is a little better, but doesn't fire until I move the mouse. IsEnabledChanged never fires because the Child Window doesn't disable anything. Checking every child window when it closes to see if Home has been clicked isn't easy because of the sheer number of places where you can open child windows, and several of them are nested within User Controls where I couldn't even easily access DialogResult. Any idea how I could do this?
Also I should note that I want each of the windows to close one by one, from top down, because each window that closes does its own verification to see if it should warn the user before closing (giving the user the option to cancel closing)
TopMost is a bool property which is either set to true or false and as far as I'm aware, there is no public property like Z-Index that will tell you the order in which your Windows were set to TopMost. However, there is a simple solution... just maintain a static int variable that will register this order. Each time you add a new Window, set the number into its Tag property:
Window childWindow = new Window();
childWindow.Tag = currentWindowNumber++;
...
childWindow.ShowDialog();
Then, when you want to close them in order, you can just do something like this:
foreach (Window window in Application.Current.Windows.OfType<YourWindowType>()
.OrderBy(w => (int)w.Tag))
{
((AnimationWindow)window).CloseWindow();
}
I want enable my parent window on closing of child window. When i use enable property it doesn't work for me. form parent to child it works, my parent window disabled.
Try this:
In parent Window:
childWindow.Closed += ChildWindowClosed;
...
private void ChildWindowClosed(object sender, EventArgs e)
{
IsEnabled = true;
}
Now, when the child Window closes, the parent Window.IsEnabled property will be set to true.
However, you probably shouldn't do this anyway... there could be negative consequences to disabling the main Window. If you just want to temporarily 'lock' the parent Window while the child Window is open, then all you have to do is to open the child Window as a dialog, like this instead:
childWindow.ShowDialog();
I'm working on this WPF project. The main window has 8 buttons which all open another window. What I'd like to do, is when the user clicks one of the buttons, the main window is hidden(not closed), and the secondary window is open. Now, when the secondary window is closed, I want to unhide my main window. Here's the code I've got.
public WndwProjectSetup(Window mainWindow)
{
InitializeComponent();
_mainWindow = mainWindow;
_mainWindow.Visibility = Visibility.Hidden; // hides main window
}
private void WindowClosing(object sender, CancelEventArgs e)
{
_mainWindow.Visibility = Visibility.Visible; // unhides main window
Close(); // close Project Setup window
}
This seems simple and straight forward to me. Yet, I get this error:
Cannot set Visibility to Visible or call Show, ShowDialog, Close, or
WindowInteropHelper.EnsureHandle while a Window is closing.
My question is, why is this not acceptable? What do I need to look into to figure out how to do this?
You don't need to call Close, it's already closing. (If anything you can cancel it by setting e.Cancel to true)
How can i keep my main window maximized in wpf app.
when i open second window and do something in that and close it, my main window get minimized.
please help me.
in fact i have three window:
A main window that application start with that.
A second window that shows a list of entities. with edit, delete and add button.
The third window that is for editing the selected entity.
when i close the third and second window, my main window get minimized.
i use the below code for opening second and third windows:
SecondWindow win = new SecondWindow();
win.Owner = this;
win.ShowDialog();
thanx.
I think Your problem is: something about how you show Main-dialog not other Forms!. Can you provide how Show Main-dialog?
also you can try use WindowState="Maximized" on your Main-dialog.
When I open from the main screen of the application a child form I also display a user control that should be displayed until the child form is closed or the user closes it. If I set the child form to be usercontrol's parent, the user control is not displayed (so the parent of the user control is the desktop). I used SetWindowPos with HWND_TOPMOST and I get the correct behaviour. now when I close the child form I want the usercontrol to close as well. Do you know what I shoud do to get this?
thanks,
When you Show() the child you can also subscribe to it's Closed or Closing event and use that to properly close the UC.
var f = new ChildForm();
// show userctl
f.Closed += MainForm_ChildClosed_Handler;
void MainForm_ChildClosed_Handler(object s, EventArgs e)
{
// close/hide userctl
}
If you keep a reference towards your usercontrol, you should be able to Close (or even Dispose) it from the Closingevent of his parent.
I don't know what you are doing, a UserControl is meant to be a child control, it should be embedded in a Form. It is technically possible to turn a UC into a top level window (like Form), you'd have to set the TopLevel property to True. That however isn't very productive, the window doesn't have the chrome to make it friendly to use. It is missing the title bar, no easy way for the user to move it around, minimize it, close it. And no easy way to solve focus problems, it can disappear behind another window with no easy way for the user to bring it back to the front. Making it top-most is a hack of sorts to get at least the focusing problem solved, but at rather a deer cost.
Just use the UC as it was intended to be used: put it inside a form. That form should probably be the child form you open. It could also be a third form, say a tool window. Use the Show(owner) overload to keep its Z-order in check and cannot do the disappearing act. That also causes it to automatically close when the owner closes.