I have a problem with one of my UserControl Windows.
I have a MainWindow and when a specific situatuin appears, another UserControl will open.
It has two Buttons which sends a command and after that it should be closed.
Right now it only opens the Window and sends the command, but doesn't close it afterwards.
I hope you can help me.
Code:
xaml:
C#:
Code to open the UserControl:
Window window = new Window();
window.Content = new MsgBox();
window.ShowDialog();
Button declaration:
public DelegateCommand OkBtn { get; set; }
Buttonfunction added to button:
OkBtn = new DelegateCommand<object>(OkBtnFkt);
In this Buttonfunction there should be something like: window.Close();
What I have tried:
Window.Close();
Sends the Window.close through the dispatcher to the UI
You could set the DialogResult of the window. What you actually have is a window with a UserControl inside. You could make a MsgBoxWindow to derive from Window. Place your control and a Close Button inside. Then if close button is clicked you can set the DialogResult.
https://msdn.microsoft.com/en-us/library/system.windows.window.dialogresult(v=vs.110).aspx
Anyway why don't you use MessageBox?
I found an answer.
This post had the answer for me :
http://www.codeproject.com/Questions/91746/Close-WPF-user-control-inside-WPF-form-application
Related
I was just curious to know about creating a popup which generates another popup BEHIND it, whenever a button is clicked.
Let me explain more:
I have a button Bu in the main window.
When I click this button, a popup say Pop_a with a button Bu_a should appear.
When I click button Bu_a a popup Pop_b should be opened, but behind Pop_a.
In Pop_a, within the button click handler, call this.Activate() after the line that shows the Pop_b window.
It should look something like this.
public partial class Pop_a : Window
{
public Pop_a()
{
InitializeComponent();
}
private void Bu_a_Click(object sender, RoutedEventArgs e)
{
var pop_b = new Pop_b();
pop_b.Show();
this.Activate();
}
}
This will put Pop_a back on top of all other windows, after Pop_b has been shown.
If you call this.Activate() before calling pop_b.Show(), Pop_a will be put on top, and then Pop_b will be shown, which puts Pop_b on top, so the order of these method calls matters. Show new window, then Activate the window you want on top.
Is it possible to switch or activate other windows when modal window is open?
Window1 window = new Window1();
window.ShowDialog();
If I understood you correctly you can do something like the following:
Window1 window = new Window1();
window.Activate();
This will activate window and bring it to the foreground.
I don't think it's possible to set the control back to parent form from a modal window(That's the point of using Modal Window). You can use non modal window.
window.Show();
If you use window.Activate(); it will not show the form.
I'm work on WPF project, let say I have two windows window1,window2, window1 call window2: in window1 code behind :
Window2 _window2 = new Window2();
_window2.ShowDialog();
What I want to know how I close first window (window1) after the secound window (window2) is opened ?
If "Window1" is not your main window you can just type this
Window2 _window2 = new Window2();
_window2.Show();
this.Close();
If your form is the main form than refer
(Windows Forms: Change application mainwindow at runtime)
Before you can close the main window (if it is), then you need to tell the app first which is the main window. Depending upon your shut down mode, if you close the main window, you shut down the app. You can set this using Application.Current.MainWindow property. See Here.
If you wish to close the first window after opening the second, you will need to hook up an event, or better still use a decoupled messaging system to inform the first window the second has opened.
Form1:
Window2 _window2 = new Window2();
this.Hide(); //Hides Form 1
_window2.ShowDialog();
Also see:
Close a Window from Another In Wpf
if you wanna have window1 closed.
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)
I have an application on C#/WPF that I`m developing ... I have a btn named About which should open a new window that contains details about the application or whatever I will put in it.
When I click the btn, a new window (about) opens, when I click again, while the new window (about) is opened another one is opened, how can I prevent this from happening, I also want the application to be disabled when the About window is opened and to be enabled when the About window is closed, Like most of the applications when about is clicked.
You should use the ShowDialog Method: http://msdn.microsoft.com/en-us/library/system.windows.window.showdialog.aspx
Code Sample:
// Instantiate window
AboutWindow aboutWindow = new AboutWindow();
// Show window modally
// NOTE: Returns only when window is closed
Nullable<bool> dialogResult = aboutWindow.ShowDialog();
Just use ShowDialog() method instead of Show()
AboutWindow aboutWindow = new AboutWindow();
aboutWindow.ShowDialog();