"Swapping" Windows in C# WPF instead of opening a new one? - c#

I'm trying to create a "Guide" app for a videogame. I want a button to clear everything in the window and show something else instead of creating another window. I've been using the code:
Window1 w1 = new Window1();
w1.Show();
this.Close();
Instead of this method, which closes the current window and opens a new one, is there a way to clear everything in the window and pop up other information?

You could create a Frame in your MainWindow and display different pages.
Xaml Frame:
<Frame x:Name="MainFrame" NavigationUIVisibility="Hidden" />
After that you create a new Page-Control and navigate to it
var page = new CustomerPage();
MainFrame.Navigate(page);

Related

How to close Window in WinUI3?

I have an app that consists of 2 parts. 1st part is Login form, where user needs to enter login and password. If they are correct, it start "Editor" window where user can work.
For now in order to launch second window I use:
var editorWindow = new EditorWindow();
editorWindow.Activate();
The problem is that Login window is still there, and while it is not critical, I still want to close it after Login is done.
First time I tried to add Window.Close() after opening the 2nd window in the .cs file of 1st Window, so
var editorWindow= new EditorWindow();
editorWindow.Activate();
var oldWindow = new MainWindow();
oldWindow.Close();
Which resulted Attempted to read or write protected memory eror.
I tried to do it in the 2nd Window .cs file like this:
this.InitializeComponent();
var oldWindow = new MainWindow();
oldWindow.Close();
Which resulted the same error
So how can I do this properly?
If you open the second window in the code-behind of the first window, you should be able to just call this.Close() right after you've called Activate() on the new window:
var editorWindow= new EditorWindow();
editorWindow.Activate();
this.Close();
If you open the EditorWindow from somewhere else, you need to get a reference to the first window to be able to close it. You could for example use a variable in the App class for this as suggested here.

WPF MainWindow doesn't close with .Close

I'm working on a small project where I have a login window and when the user is autheticated it will open a new window, that works but after the user logged in I want to close the Login window how ever, the main window doesn't close.
MainWindow mainWindow = new MainWindow();
mainWindow.Close();
Loader loader = new Loader();
loader.Show();
You are initializing a new instance of a Window and close it immediateley afterwards.
It looks like your mainWindowis not the login window you want to close. If you want to close the current Main Window of you application, you can use:
App.Current.MainWindow.Close();
Make sure you also set a new Main Window. I assume you wanted to do something similar to this, also assuming "loader" is your new Window:
private void Login(){
App.Current.MainWindow.Close();
Loader loader = new Loader();
App.Current.MainWindow = loader;
}

Opening a windows relative to its tab in WPF

I've a WPF application that makes use of AvalonDock for opening multiple tabs ...each tab represents a function and has it's own controls .. The single tab is opened via menu.
Sometimes I need to show a window in the current tab and I use the
MyWindow w = new MyWindow();
w.Show(); //w.ShowDialog();
this opens a popup(/modal) but when I switch the tab (in the case it's not modal) I have the Window still open... what I need is to "force" the window to be shown only in it's tab.
I've tried setting the Owner of the window but when I do
var wnd = Window.GetWindow(docuentPane);
I got the main window of the app...
is it possible to do what I want to achieve in WPF? if yes how?
Thanks in advance

Change content of WPF Window

When I click on a Button on my navigation bar, a new Window opens. For example:
private void btFinanzen_Click(object sender, RoutedEventArgs e)
{
Finanz mw = new Finanz(login);
mw.Show();
}
Now, I don´t want to show it in a new window. Instead of that, I want it to be shown at the same window. I tried with the following:
private void btFinanzen_Click(object sender, RoutedEventArgs e)
{
Finanz mw = new Finanz(login);
//mw.Show();
this.Content = Finanz f;
}
but it failed.
What I should say is, that I´ve many of different windows now and want them all to be shown at one window - each by clicking a different button.
How can I manage this?
What you have to do is to use a UserControl rather than a Window for your replaceable content. Add a new UserControl for each possible content you want to show in this.Content to your project. Add the content you had previously in the second window to the appropriate UserControl. After that you can simply instantiate the new control in your code and assign it to your content area of your main Window.
For example you create a UserControl ctlFinanz with the content of your previous Finanz window. Now you simply write:
this.Content = new ctlFinanz(login);
That's all :-)
I had the same problem and decided to create a seperate usercontrol for every window, put them in my wpf and set the visibility of all windows except the active one to Collapsed.
To me, this sounds like place to use a frame with pages. Instead of making lots of windows, make one window with a frame, then put the content into pages. Then all you have to do is change the frame's .Content to a different page each time you press a navigation button.
Create a main content with a UserControl that I set into the window constructor. And then I change the content with other UserControl depending on your needs.
MainWindow constructor:
this.Content = new UserControlMainContent();
From UserControlMainContent, when I click on a button by example:
Window window = Window.GetWindow(this); // Get the main window
window.Content = new OtherUserControlContent();

How do you send information from one window to another in the same program?

In the program I am trying to build, I have a menu button that opens a second window. The user puts information into the second window, presses the "Done" button, and the information is transfered into the main window. The problem I am having is opening the second window. I have both windows build in xaml files in Visual Studio but I can't find a way to show the second window. Using "Window window = new Window" does not fit my needs because 1) I already have the second window built and 2) I have tried this and I cannot figure out how to add children to the window; there is no window.children nor any grid to put the children into. Thank you in advance!
Moments after I pressed post, I thought of something I hadnt tried:
"WindowAdd add = new WindowAdd; //WindowAdd being the second window
add.Show();"
This does exactly what I want it to do. The next problem I have is sending the information the TextBoxes into the MainWindow. I am thinking cookies might work but am unsure. Anyone have any thoughts? Thanks in advance!
You need to create the Window in code, but instead of doing:
Window window = new Window();
You should use:
Window2 window = new Window2(); // Assuming the window's class name is Window2
This will construct and initialize an instance of your new window class, defined in XAML. Once you've done this, you can open the window and you'll see all of your controls.

Categories

Resources