如何使用WPF窗口作为一个消息? [英] How to use WPF window as a messagebox?

查看:88
本文介绍了如何使用WPF窗口作为一个消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个小窗口,一些样式。现在我想用它作为完全像一个消息框。我怎样才能实现呢?

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

编辑:我是新来的WPF。我打过电话就在这样的主窗口。

I am new to WPF. I tried calling it in the main window like this.

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

现在的问题是新产生的窗口消失仅次于主窗口没有让我在里面点击操作按钮。

The problem is the newly generated window disappears just behind main window without letting me to click on the action button in it.

推荐答案

使用的ShowDialog 而不是显示方法弹出消息框的窗口。这样一来用户就的hve搬回到主窗口之前,先关闭弹出或消息框窗口

Use ShowDialog instead of Show method to popup the messagebox window. In this way user will hve to first close the popup or message box window before moving back to main window

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

修改

您显然希望能有结果,回到主窗口吧?你可以通过几种方式做到这一点。一个简单的方法可能会暴露在主窗口的公共方法

You obviously would like to have result back in the main windows right? you can do it in several ways. One simplest way could be to expose a public method in MainWindow

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

创建MessageWindow的构造函数,采取主窗口中的参数

Create a constructor of MessageWindow that take MainWindow in 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();

这篇关于如何使用WPF窗口作为一个消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆