Skip to content

Commit

Permalink
changes from: Pr/1467 Get the dialog from a MetroWindow #1739
Browse files Browse the repository at this point in the history
i don't know why this pr doesn't works
  • Loading branch information
punker76 committed Jan 8, 2015
1 parent 4a632d2 commit 55c1de1
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
17 changes: 17 additions & 0 deletions MahApps.Metro/Controls/Dialogs/DialogManager.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
Expand Down Expand Up @@ -355,6 +356,22 @@ public static Task HideMetroDialogAsync(this MetroWindow window, BaseMetroDialog
}).Unwrap();
}

/// <summary>
/// Gets the current shown dialog.
/// </summary>
/// <param name="window">The dialog owner.</param>
public static Task<TDialog> GetCurrentDialogAsync<TDialog>(this MetroWindow window) where TDialog : BaseMetroDialog
{
window.Dispatcher.VerifyAccess();
var t = new TaskCompletionSource<TDialog>();
window.Dispatcher.Invoke((Action)(() =>
{
TDialog dialog = window.metroDialogContainer.Children.OfType<TDialog>().LastOrDefault();
t.TrySetResult(dialog);
}));
return t.Task;
}

private static SizeChangedEventHandler SetupAndOpenDialog(MetroWindow window, BaseMetroDialog dialog)
{
dialog.SetValue(Panel.ZIndexProperty, (int)window.overlayBox.GetValue(Panel.ZIndexProperty) + 1);
Expand Down
10 changes: 10 additions & 0 deletions Mahapps.Metro.Tests/CustomDialogTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,20 @@ public async Task ReceivesDataContext()

await TestHost.SwitchToAppThread(); // No idea why we have to do this again

Assert.Equal(await window.GetCurrentDialogAsync<CustomDialog>(), dialog);
Assert.NotNull(await window.GetCurrentDialogAsync<BaseMetroDialog>());
Assert.Null(await window.GetCurrentDialogAsync<MessageDialog>());

dialog.DataContext = vm;
var textBlock = dialog.FindChild<TextBlock>("TheDialogBody");

Assert.Equal(vm.Text, textBlock.Text);

await window.HideMetroDialogAsync(dialog);

await TestHost.SwitchToAppThread(); // No idea why we have to do this again

Assert.Null(await window.GetCurrentDialogAsync<MessageDialog>());
}

private class TheViewModel
Expand Down

0 comments on commit 55c1de1

Please sign in to comment.