WPF MessageBox window style

匿名 (未验证) 提交于 2019-12-03 01:59:02

问题:

How to apply the default Windows style to the standard MessageBox in WPF?

For example, when I execute next code:

MessageBox.Show("Hello Stack Overflow!", "Test", MessageBoxButton.OKCancel,      MessageBoxImage.Exclamation); 

I'm getting message box:

But in WinForms everything is OK with style:

MessageBox.Show("Hello Stack Overflow!", "Test", MessageBoxButtons.OKCancel,      MessageBoxIcon.Exclamation); 

回答1:

According to this page, WPF picks up the old styles for some of the controls.

To get rid of it, you have to create a custom app.manifest file (Add -> New item -> Application Manifest File) and paste the following code in it (right after the /trustInfo - Tag ):

   

Then you have to compile your solution with this app.manifest (set it in the project properties -> Application -> Point to the new manifest in "Icons and manifest").

If you start your application now it should look like the WinForms- MessageBox.



回答2:

Also, for WPF I would recommmend using the Extended WPF Toolkit which has a WPF messagebox



回答3:

The reason that the WinForms one works the way that it does is because visual styles are turned on (i.e. using Common Controls v6) in its Main function. If you remove the call to System.Windows.Forms.Application.EnableVisualStyles(), then the WinForms Message Box will look just like the WPF one.

This doesn't happen for a WPF app, possibly because all of the WPF controls are rendered so there is no need to use the new version of Common Controls.

You might try calling EnableVisualStyles() somewhere in the start up of your WPF application. I don't know if it will work or not, but it's worth a try. This will require a reference to System.Windows.Forms, though.



回答4:

as how i triggered it, "redirecting" the usual references to the Forms ones (they work the same, but are named differently):

using MessageBox = System.Windows.Forms.MessageBox; using MessageBoxImage = System.Windows.Forms.MessageBoxIcon; using MessageBoxButton = System.Windows.Forms.MessageBoxButtons; using MessageBoxResult = System.Windows.Forms.DialogResult;  namespace ... class ...      public MainWindow()     {         InitializeComponent();          System.Windows.Forms.Application.EnableVisualStyles();     }      public void do()     {         // updated style, but good syntax for a later solution         MessageBox.Show("Some Message", "DEBUG", MessageBoxButton.OK, MessageBoxImage.Question);     } 

... the manifest solution did not work for me.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!