I have set the Visibility property of the main window to Hidden and added the following in Window_Loaded:
private void Window_Loaded(object sender, RoutedEventArgs e)
{
this.Visibility = System.Windows.Visibility.Visible;
}
But it doesn't show up the Window. Any specific reason for this?
The window is not loaded until it is shown, as per your code it will not be shown until it is loaded. Obivously this cannot work like that, right?
I was having an issue with this as well and it seems changing the visibility alone on a main window doesn't work as H.B. pointed out. For my case, I wanted to not show the window until it was completely loaded and was able to achieve that by using the property I've linked to here, along with the Show() and Hide() functions on the Window object.
System.Windows.Window.ShowActivated
When initializing the window object don't set visibility to hidden, instead follow the next steps
Set the ShowActivated property to false this.ShowActivated = false;
Call the Hide() function on the window object this.Hide();
On your window loaded function from your original example call this.Show();
It is also possible in some WPF apps for the this reference not to work as expected, however if this is the case go to the XAML and find the name property of the window. You should be able to ref the window from the code via that name. Ex.
<Window x:Name="MainWindow">
//Code Behind Below
MainWindow.ShowActivated = false;
Related
I am working on a personal project. At one point in the project you have a menu with some buttons, and one of those buttons will:
Hide current form
Create a second form and show it through ShowDialog(this)
When the second form closes, the first form will be shown again with Show()
My issue is, that after showing the first form again, a certain border is not being redrawn. The object with this border is called pnlInfoBoard.
Here is some screenshots to show the problem:
The border appears to become transparent, and this only happens after hiding and showing the form.
I found out that the second form has no influence on this, it is only the act of hiding and showing the form. I had tried providing a fix by explicitly invalidating and updating pnlInfoBoard however, this appears to have no effect.
Here is the code:
private void btnInstructions_Click(object sender, EventArgs e)
{
this.Hide();
//Instructions frmInstructions = new TowerTitans.Instructions();
//frmInstructions.ShowDialog(this);
this.Show();
pnlInfoBoard.Invalidate();
pnlInfoBoard.Update();
}
Why would this happen?
How do I fix it?
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 am writing an application using WPF. I would like such an action: when I press down mouse button on a button, another window appears, when I release mouse button wherever, this window hides. That is my code so far:
XAML:
<Button Margin="0,0,0,0" Name="button_wykres" PreviewMouseUp="button_wykres_PreviewMouseUp" PreviewMouseDown="button_wykres_PreviewMouseDown">
C#:
private void button_wykres_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
omww.Top = this.Top+50;
omww.Left = this.Left +180;
omww.Show();
}
private void button_wykres_PreviewMouseUp(object sender, MouseButtonEventArgs e)
{
omww.Hide();
}
The problem is that new window (omww) appears under the main one, but I need it on top. When I tried other events, i.e. button.Click window is showed above the old one, as I want, and that confuses me. However, Click event doesn't meet my needs. I'd be grateful if anybody help me.
As I see it, you have two possible options for making the child Window appear on top of the parent Window. The first was mentioned by #Viv in a comment and that is to set the TopMost property on the child Window to true:
omww.Topmost = true;
The second option would be to set the Owner property of the child Window to the parent Window (if there is a direct relationship between them):
omww.Owner = this; // if called from the parent Window code behind
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.
For my WPF application, I am storing several user settings like window position, window state, and whether or not to display a welcome dialog. The problem is that while everything is loading up, I see a lot of flashing and flickering as the windows are loaded in, and then more flickering when the window is maximized after reading in the settings.
I am already using the built-in WPF PNG splash screen functionality, but is there a way to completely hide the rendering of all windows until everything is fully loaded in?
Edit the Application.xaml, remove the StartUpUri, instead set the StartUp event handler.
In Application.xaml.cs, edit the startup event handler to display the splashscreen, load your resources, create everything, then create the main window and show it.
<Application
...
StartUp="OnStartUp"
/>
And:
private void OnStartUp(Object sender, StartupEventArgs e)
{
var settings = LoadSettingsFrom... // Call your implementation of load user settings
// Example only, in real app do this if's section on a different thread
if (settings.doShowSplashScreen)
{
var splashScreen = new SplashScreen();
splashScreen.Show();
}
// Load and create stuff (resources, databases, main classes, ...)
var mainWindow = new mainWindow();
mainWindow.ApplySettings(settings); // Call your implementation of apply settings
if (doShowSplashScreen)
{
// send close signal to splash screen's thread
}
mainWindow.Show(); // Show the main window
}
You can set the windows WindowState to Minimized, then handle the ContentRendered event and set the WindowState to Normal or Maximized.
There are functions , BeginInit and EndInit, if you change properties inside these functions like..
BeginInit();
...
... // Do your code Initialization here...
...
EndInit();
then your window will not render until the EndInit() is called, it will not flicker.
When does this loading occur? Code executed in the main Window's constructor should execute before the window is shown; if you load any required resources there, you should not see any flickering.