Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
        }
    }
like image 864
user3342256 Avatar asked Dec 03 '22 11:12

user3342256


1 Answers

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;
like image 166
Talha Talip Açıkgöz Avatar answered Dec 05 '22 00:12

Talha Talip Açıkgöz