WPF event when a window is no longer on top - c#

I have a WPF window (window1) whose owner is window2. If a user clicks on window2, or the desktop, or anything else to make window1 not on top of the z-order, I want to set window1's visibility to hidden. I.e., the window either needs to be on top, or hidden. Is this possible?

Yes.
public Window1()
{
InitializeComponent();
this.Deactivated += new EventHandler(Window1_Deactivated);
}
void Window1_Deactivated(object sender, EventArgs e)
{
Visibility = Visibility.Collapsed;
}
Note that this will also remove it from the TaskBar.

Related

Restored WPF Window

I have an option to start my mainwindow minimized. The window is set to SizeToContent="WidthAndHeight". when I click to restore the window it opens larger than the initial WidthAndHeight. I use the following in the MainWindow.xaml.cs
private void WindowStart()
{
if (LocalSystem.StartMinimized)
{
WindowState = WindowState.Minimized;
ShowInTaskbar = true;
}
if (LocalSystem.StartOnTop)
{
Topmost = true;
}
Activate();
}
Consider keeping track of the state change of your WPF window. Add an event handler of the StateChanged event of your Window and keep track of the Window width and height.
You could keep track of the size of the window like this:
public MainWindow() {
InitializeComponent();
_lastState = this.WindowState;
_initialWidth = this.ActualWidth;
_initialHeight = this.ActualHeight;
this.StateChanged += Window_StateChanged;
}
WindowState _lastState;
double _initialwidth, _initialHeight;
private void Window_StateChanged(object sender, EventArgs e)
{
if (this.WindowState != _lastState)
{
if (_lastState == WindowState.Minimized && this.WindowState == WindowState.Maximized){
this.Width = _initialWidth;
this.Height = _initialHeight;
}
_lastState = this.WindowState;
}
}
Note the use of ActualHeight and ActualWidth. The Window actual dimensions will be known after InitializeComponent is run inside its constructor. A restored or unminized, as you so eloquently put it, window is window where the State goes from WindowState.Minimized to WindowState.Maximized.
Note that we try to minimize the amount of code behind in WPF apps, as a best practice is to use MVVM. You could though consider creating a WPF behavior class for restoring initial size of Window, after all it is a generic behavior.

Control keys not working while window is disable and again Enable

I have a XAML MainWindow and Child User control loaded on that window. On button click event in user control I disable mainWindow as Well as User Control till a work on button click is not finished. When I again updating the property from User control to enable the window again, now user control keys are not working, because while debugging its showing IsEnabled Property of window/user control is still disable. I am also forcefully updating user control property this.IsEnabled = true; but still its not updating IsEnabled property of class true.
How could I update the mainwindow and user control property to enable or how could my Control keys can work again.
If you set mainWindow.IsEnabled to false, you cannot set userControl.IsEnabled to true. You have to set the mainWindow.IsEnabled to true first. You cannot have an enabled control inside a disabled control.
I wrote a simple WPF app with a user control with a button. The main window has the user control and the code for the user control is:
public partial class MyUserControl : UserControl
{
DispatcherTimer m_timer = new DispatcherTimer();
public MyUserControl()
{
InitializeComponent();
m_timer.Tick += Timer_Tick;
m_timer.Interval = TimeSpan.FromSeconds(4);
}
private void Timer_Tick(object sender, EventArgs e)
{
Window window = Window.GetWindow(this);
window.IsEnabled = true;
m_timer.Stop();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Window window = Window.GetWindow(this);
window.IsEnabled = false;
m_timer.Start();
}
}

How to disable all resize possibilities for a window?

I have a line of code to forbid all possibilities to resize the window of my application.
this.ResizeMode = ResizeMode.NoResize;
Through this the maximize and minimize button on the top right of the window are not shown and you cannot maximize the window by double clicking the menubar.
I thought it works but then I found out that you can minimize the window with a double click on the menubar. I was very confused that this is possible. Has someone an answer to this question that you can not maximize the window with the double click but it is possible to minimize it?
You can easily prevent minimization my detecting the change with the Resize event and setting it back to its original size.
NOTE: You will see the window minimize briefly and then come back.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.Resize += new EventHandler(Form1_Resize);
}
void Form1_Resize(object sender, EventArgs e)
{
if (WindowState == FormWindowState.Minimized)
{
this.WindowState = FormWindowState.Normal;
}
}
}

Cannot change object property from other window

I have the main window, and another window. in the 2nd window i've created new canvas, and i want to change its properties from the main window, i've failed with this try:
this is the 2nd window's class:
public partial class window2 : Window
{
public Canvas painting = new Canvas();
public window2()
{
}
}
and here i try to change its property from the main window:
window2 paint = new window2();
private void button1_Click(object sender, RoutedEventArgs e)
{
paint.painting.Background = Brushes.Black;
}
when i click the button it does nothing.
Edit:
i think it would be better if i'll use the Application.current.properties and store the canvas object, but i don't know how to use it, i tried this:
Application.Current.Properties["p1"] = painting;
now how can i set properties from the main window with using the "p1" variable i've just created? i tried p1.background but i cant use p1 as variable, so how do i do it?
Your window2 constructor should contain this:
this.AddChild(painting);
Whenever you create a new control (like Canvas) you should set its parent container.
This is my code from window2:
public Canvas painting = new Canvas();
public window2()
{
this.AddChild(painting);
}
And mainwindow:
private void button1_Click(object sender, RoutedEventArgs e)
{
window2 w = new window2();
w.Show();
w.painting.Background = Brushes.Black;
}
What I believe you are saying is that you have an undetermined number of canvases and you want to access them all. I suggest you keeping them in a List of canvases or in a HashTable (you need to use System.Collections namespace). Also don't forget to set the parent container.
A Canvas is a little odd as far as WPF controls go. It likely has a zero size, so you're not seeing the change. Try hard-coding a size to check whether the code is working. In the window2 constructor do this:
painting.Width = 100;
painting.Height = 100;

How to close a hidden window (WPF application)?

In my application (WPF) i have this window:
public partial class Window1 : Window and in the Xaml x:Class="WpfApplication1.Window1"
Now, when i switch to and from the main to window 1 and back, i us the Visibility.Hidden and Visibility.Visible to hide them and make them show again to the user.
What i try to do now, is make a test button in the main window, that says: Close Window1.
This window is hidden, but i really want to close it in the background.
at first i though to just use the Window.Close(); but that does not seem to do the trick.
So, how should i do this in a correct way ?
Thank you very much in advance.
EDIT 1 - making question more clear
To open the window1 in my Main window, i use this part
Window1 W1 = null; // Initialise Field.
public void CalcTabel_Click(object sender, RoutedEventArgs e)
{
if (W1 == null)
{
W1 = new Window1();
W1.Hoofdmenu = this;
W1.checkLang();
W1.Show();
}
else
{
W1.checkLang();
W1.Visibility = Visibility.Visible;
}
this.Visibility = Visibility.Hidden;
}
On window 1 there is a Back button, that has this snip-it of code in it (Where "Hoofdmenu" us the main window):
Hoofdmenu.updateStatistics();
Hoofdmenu.Visibility = Visibility.Visible;
this.Visibility = Visibility.Hidden;
But again, this time when standing in the main window (so window 1 is hidden) i want to close down that window 1. but using W1.Close() does not seem to work. So i am looking for a way to Close that window 1, not change its visibility.
EDIT 2 - Solution
So using W1.Close(); did not work, although a small change this.W1.Close(); did work in fact :)
You can create object of Form2 in window and intialize its visiblity to false.
On click of button you can simpliy say
public partial class MainWindow : Window
{
private Window1 window2;
public MainWindow()
{
InitializeComponent();
this.window2 = new Window1();
this.window2.Show();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
this.window2.Visibility = System.Windows.Visibility.Hidden;
}
}
to make it visible again
after reading your code, thats not possible the way you want it. the window1 instance is local object. so you cant reach it out side if this method.
the best was is to have a close button on window1 with this.close()
or you create a global instance in your main window and then check if not null then close it.

Categories

Resources