Search code examples
desktop-applicationwinui-3winui

WinUI3 Window -> ShowDialog


I am trying to show the window as ShowDialog (same as WPF) in the WinUI3, but found that the currenly present method window.Activate() does not behave the same as ShowDialog().

1.) Is there any way to acheieve this in WinUI3, to show window as ShowDialog() (as in WPF).

2.) I tried to use the ContentDialog, but the limitation is that, I can only one dialog at a time. Suppose , A content dialog contains various input fields or other controls, Now If I want to pop up a messagedialog on top of that, I cannot acheive this, because a ContentDialog is already open.

Is there any way to show a MessageDialog in buttonclick event of ContentDialog.


Solution

  • There's an issue regarding to this. So for the moment, you can't create nested ContentDialogs.

    As a workaround (for the moment), you can use the TeachingTip like this. Should be enough to show messages.

    MainPage.xaml

    <Page
        x:Class="ContentDialogTests.MainPage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:local="using:ContentDialogTests"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"
        mc:Ignorable="d">
        <Grid>
            <Button
                HorizontalAlignment="Center"
                VerticalAlignment="Center"
                Click="ShowContentDialogButton_Click"
                Content="Show parent dialog" />
            <ContentDialog
                x:Name="ContentDialogControl"
                Title="ContentDialog"
                CloseButtonText="Close">
                <Button
                    Click="ShowTeachingTipButton_Click"
                    Content="Show teaching tip" />
            </ContentDialog>
            <TeachingTip
                x:Name="TeachingTipControl"
                Content="Teaching tip"
                IsOpen="False"
                PreferredPlacement="Center" />
        </Grid>
    </Page>
    

    MainPage.xaml.cs

    using Microsoft.UI.Xaml;
    using Microsoft.UI.Xaml.Controls;
    using System;
    
    namespace ContentDialogTests;
    
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
            this.Loaded += MainPage_Loaded;
        }
    
        private void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            this.TeachingTipControl.Closed += (sender, args) => this.ContentDialogControl.IsEnabled = true;
        }
    
        private async void ShowContentDialogButton_Click(object sender, RoutedEventArgs e)
        {
            _ = await ContentDialogControl.ShowAsync();
        }
    
        private void ShowTeachingTipButton_Click(object sender, RoutedEventArgs e)
        {
            this.ContentDialogControl.IsEnabled = false;
            this.TeachingTipControl.IsOpen = true;
        }
    }