在 WPF 中这没有问题。您只需为 MessageBox.Show 调用指定 Window所有者参数。拥有的消息框本质上是模态的。例如,使用 Show 的这个重载版本来自定义消息框:
public static MessageBoxResult Show(Window owner, string messageBoxText, string caption, MessageBoxButton button, MessageBoxImage icon);
这是非常简单的(虽然肯定有效:))示例:
MainWindow.xaml:
<Window x:Class="WpfApplication2_ModalMessageBox.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Button VerticalAlignment="Center" HorizontalAlignment="Center"
Click="Button_Click">Modal</Button>
</Grid>
</Window>
MainWindow.xaml.cs:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show(this, "Text", "Caption", MessageBoxButton.OK, MessageBoxImage.Information);
}
}