How to get MessageBox.Show() to pop up in the middle of my WPF application?

前端 未结 6 1451
忘掉有多难
忘掉有多难 2020-12-17 08:39

I have a 700w x 300h WPF applciation and can drag it anywhere on my large screen.

When my application executes:

MessageBox.Show(\"Sorry, this functio         


        
6条回答
  •  旧巷少年郎
    2020-12-17 09:31

    If you're open to using external libraries, one option is the Xceed WPF Toolkit which has a MessageBox that will center properly. It is also very consistent with WPF in general, e.g. it can be styled from XAML.

    https://github.com/xceedsoftware/wpftoolkit/wiki/MessageBox

    Usage

    The MessageBox mimics the behavior of the System.Windows.MessageBox closely. You use similar syntax to create and show a message box.

    MessageBoxResult result =  Xceed.Wpf.Toolkit.MessageBox.Show("Hello world!", "Extended WPF ToolKit MessageBox", MessageBoxButton.OK, MessageBoxImage.Question);
    
    MessageBoxResult result =  Xceed.Wpf.Toolkit.MessageBox.Show("Hello world!", "Extended WPF ToolKit MessageBox", MessageBoxButton.OK);
    
    MessageBoxResult result =  Xceed.Wpf.Toolkit.MessageBox.Show("Hello world!", "Extended WPF ToolKit MessageBox");
    
    MessageBoxResult result =  Xceed.Wpf.Toolkit.MessageBox.Show("Hello world!");
    

    The centering on parent window appears to be automatic.

提交回复
热议问题