user3342256
user3342256

Reputation: 1290

Closing Material Design DialogHost from Code

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
        }
    }

Upvotes: 5

Views: 18952

Answers (3)

OnkuMaa
OnkuMaa

Reputation: 11

I also had issues closing a Material Design dialog from code-behind, my fix was:

DialogHost.CloseDialogCommand.Execute(true,this);

I had to pass "this" as an argument, otherwise it is not working. The additional boolean value (true) is optional and in my case used as return value of the dialog show method.

Upvotes: 1

Eduardas Šlutas
Eduardas Šlutas

Reputation: 307

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.

Upvotes: 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;

Upvotes: 21

Related Questions