How to use a WPF window as a mailbox?

I created a small window with some style. Now I want to use it just like a MessageBox. How can i achieve this?

Edit: I am new to WPF. I tried to call it in the main window like this.

Window1 messageBox = new Window1(); messageBox.Show(); messageBox.Activate(); 

The problem is that the newly created window disappears right behind the main window, preventing me from clicking on the action button in it.

+4
source share
1 answer

Use ShowDialog instead of the Show method to pop up a message box window. Thus, the user will first close the pop-up or message box before returning to the main window

  MessageWindow message= new MessageWindow(); message.ShowDialog(); 

Edit

You obviously would like to return the result in the main windows? You can do this in several ways. The easiest way is to open a public method in MainWindow

 public GetResult(bool result) { //your logic } 

Create a MessageWindow constructor that takes a MainWindow in the parameter

  private MainWindow window; public MessageWindow(MainWindow mainWindow) { InitializeComponent(); window = mainWindow; } //now handle the click event of yes and no button private void YesButton_Click(object sender, RoutedEventArgs e) { //close this window this.Close(); //pass true in case of yes window.GetResult(true); } private void NoButton_Click_1(object sender, RoutedEventArgs e) { //close this window this.Close(); //pass false in case of no window.GetResult(false); } //in that case you will show the popup window like this MessageWindow message= new MessageWindow(this); message.ShowDialog(); 
+7
source

Source: https://habr.com/ru/post/1388101/


All Articles