title: Closing Material Design DialogHost from Code
body: <p>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.</p>
  
  <p>The entirety of my XAML:</p>
  
  <pre><code>&lt;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}"&gt;
  
  &lt;materialDesign:DialogHost Identifier="RootDialog" Loaded="DialogHost_Loaded"&gt;
      &lt;Grid&gt;
          &lt;StackPanel&gt;
              &lt;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"&gt;
                  &lt;Button.CommandParameter&gt;
                          &lt;StackPanel Margin="10"&gt;
                              &lt;TextBlock Text="Restoring..."/&gt;
                          &lt;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}"&gt;
                              &lt;materialDesign:PackIcon Height="24" Width="24" Foreground="White"&gt;
                                  &lt;materialDesign:PackIcon.Style&gt;
                                      &lt;Style TargetType="materialDesign:PackIcon" BasedOn="{StaticResource {x:Type materialDesign:PackIcon}}"&gt;
                                          &lt;Setter Property="Kind" Value="CloudSync" /&gt;
                                          &lt;Style.Triggers&gt;
                                              &lt;DataTrigger Binding="{Binding IsSaveComplete}" Value="True"&gt;
                                                  &lt;Setter Property="Kind" Value="Check" /&gt;
                                                  &lt;DataTrigger.EnterActions&gt;
                                                      &lt;BeginStoryboard&gt;
                                                          &lt;Storyboard&gt;
                                                              &lt;DoubleAnimation Storyboard.TargetProperty="Opacity" From="0" To="1" Duration="0:0:0.8" /&gt;
                                                          &lt;/Storyboard&gt;
                                                      &lt;/BeginStoryboard&gt;
                                                  &lt;/DataTrigger.EnterActions&gt;
                                              &lt;/DataTrigger&gt;
                                          &lt;/Style.Triggers&gt;
                                      &lt;/Style&gt;
                                  &lt;/materialDesign:PackIcon.Style&gt;
                              &lt;/materialDesign:PackIcon&gt;
                          &lt;/Button&gt;
                      &lt;/StackPanel&gt;
                  &lt;/Button.CommandParameter&gt;
              &lt;/Button&gt;
          &lt;/StackPanel&gt;
      &lt;/Grid&gt;
  &lt;/materialDesign:DialogHost&gt;
  </code></pre>
  
  <p></p>
  
  <p>And the code from which I need to initiate the close (this is in a view model, not the MainWindow code behind):</p>
  
  <pre><code>     private async void progressTimerTick(object sender, EventArgs e)
      {
  
          if (progress &lt; 100 &amp;&amp; cycles &lt; 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
          }
      }
  </code></pre>