Closing Material Design DialogHost from Code

13,664

Solution 1

There are 3 ways to close a dialog in MDIX:

var dialogResult = await DialogHost.Show(myDialogControl, (sender, args) =>
{
    args.Session.Close(false);
});

or

DialogHost.CloseDialogCommand.Execute(null,null);

or

DialogHostInstance.IsOpen = false;

Solution 2

Regarding @Talha answer.

I have tried your solution, however, it didn't work. On the other hand, I found a way to fix it:

var drawer = DrawerHost.CloseDrawerCommand; 
drawer.Execute(null, null); 

It looks the same, but worked for me.

Share:
13,664
user3342256
Author by

user3342256

Updated on June 12, 2022

Comments

  • user3342256
    user3342256 almost 2 years

    I'm trying to find a way to initiate the close of an active DialogHost from code, but have been unable to find the right syntax. I think the main challenge is accessing the DialogHost from a class outside of the MainWindow code behind.

    The entirety of my XAML:

    <Window x:Class="MaterialDesignTest.MainWindow"
        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:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:MaterialDesignTest"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525"
        xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
        TextElement.Foreground="{DynamicResource MaterialDesignBody}"
        TextElement.FontWeight="Regular"
        TextElement.FontSize="13"
        TextOptions.TextFormattingMode="Ideal" 
        TextOptions.TextRenderingMode="Auto"        
        Background="{DynamicResource MaterialDesignPaper}"
        FontFamily="{DynamicResource MaterialDesignFont}">
    
    <materialDesign:DialogHost Identifier="RootDialog" Loaded="DialogHost_Loaded">
        <Grid>
            <StackPanel>
                <Button Width="100" x:Name="SearchRestore" Margin="0 150 0 0" Command="{x:Static materialDesign:DialogHost.OpenDialogCommand}" materialDesign:DialogHost.DialogClosingAttached="SearchRestore_OnDialogClosing" 
                    Content="Restore" Click="SearchRestore_Click">
                    <Button.CommandParameter>
                            <StackPanel Margin="10">
                                <TextBlock Text="Restoring..."/>
                            <Button Name="CircleButton" Margin="0 50 0 0" Style="{StaticResource MaterialDesignFloatingActionButton}"
                            Command="{Binding SaveComand}" IsHitTestVisible="False"
                            materialDesign:ButtonProgressAssist.IsIndicatorVisible="{Binding IsSaving}"
                            materialDesign:ButtonProgressAssist.Value="{Binding SaveProgressButton}">
                                <materialDesign:PackIcon Height="24" Width="24" Foreground="White">
                                    <materialDesign:PackIcon.Style>
                                        <Style TargetType="materialDesign:PackIcon" BasedOn="{StaticResource {x:Type materialDesign:PackIcon}}">
                                            <Setter Property="Kind" Value="CloudSync" />
                                            <Style.Triggers>
                                                <DataTrigger Binding="{Binding IsSaveComplete}" Value="True">
                                                    <Setter Property="Kind" Value="Check" />
                                                    <DataTrigger.EnterActions>
                                                        <BeginStoryboard>
                                                            <Storyboard>
                                                                <DoubleAnimation Storyboard.TargetProperty="Opacity" From="0" To="1" Duration="0:0:0.8" />
                                                            </Storyboard>
                                                        </BeginStoryboard>
                                                    </DataTrigger.EnterActions>
                                                </DataTrigger>
                                            </Style.Triggers>
                                        </Style>
                                    </materialDesign:PackIcon.Style>
                                </materialDesign:PackIcon>
                            </Button>
                        </StackPanel>
                    </Button.CommandParameter>
                </Button>
            </StackPanel>
        </Grid>
    </materialDesign:DialogHost>
    

    And the code from which I need to initiate the close (this is in a view model, not the MainWindow code behind):

         private async void progressTimerTick(object sender, EventArgs e)
        {
    
            if (progress < 100 && cycles < 2)
            {
                if (progress == 99)
                {
                    cycles++;
                    progress = 0;
                }
    
                IsSaveComplete = false;
                IsSaving = true;
                progress++;
                SaveProgressButton = progress;
            }
            else
            {
                IsSaveComplete = true;
                IsSaving = false;
                progressTimer.Enabled = false;
                SaveProgressButton = 0;
    
                await PutTaskDelay();
    
                //Close dialog here
            }
        }
    
  • user3342256
    user3342256 almost 6 years
    Thanks! DialogHost.CloseDialogCommand.Execute(null, null); worked best for my purposes.
  • Yves Calaci
    Yves Calaci over 3 years
    Excuse me, but where do you do any of these 3 approaches?
  • Bandook
    Bandook about 3 years
    The question is about DialogHost and not DrawerHost. Talha's answer works for DialogHost as that's what OP required.
  • Eduardas Šlutas
    Eduardas Šlutas about 2 years
    DialogHost and DrawerHost have the exact syntax in order to close it.
  • Bandook
    Bandook about 2 years
    No they don't. DialogHost does not have "CloseDRAWERCommand". They have similar structure but not the EXACT same syntax. Which is why Talha's answer is on-point.
  • Bandook
    Bandook about 2 years
    @Henri Material Design is not documented like other libraries, but the code itself with the example project is exceptionally good! github.com/MaterialDesignInXAML/MaterialDesignInXamlToolkit . Best way to learn would be to get the source code & run the MainDemo.wpf project. They have shown code snippets for each control they use. Its really well done.