Skip to content

Commit

Permalink
Merge pull request #128 from punker76/fix/#117-childwindow-isopen-check
Browse files Browse the repository at this point in the history
Check if a dialog is already open and a children of a panel
  • Loading branch information
punker76 committed Oct 3, 2022
2 parents d034cba + b4742ba commit b034499
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 38 deletions.
28 changes: 17 additions & 11 deletions src/MahApps.Metro.SimpleChildWindow.Demo/MainWindow.xaml
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
<Controls:MetroWindow x:Class="MahApps.Metro.SimpleChildWindow.Demo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Controls="http://metro.mahapps.com/winfx/xaml/controls"
xmlns:simpleChildWindow="clr-namespace:MahApps.Metro.SimpleChildWindow;assembly=MahApps.Metro.SimpleChildWindow"
Title="MahApps.Metro Simple ChildWindow Demo"
Width="700"
Height="600"
GlowBrush="{DynamicResource MahApps.Brushes.Accent}"
WindowStartupLocation="CenterScreen">
<mah:MetroWindow x:Class="MahApps.Metro.SimpleChildWindow.Demo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mah="http://metro.mahapps.com/winfx/xaml/controls"
xmlns:simpleChildWindow="clr-namespace:MahApps.Metro.SimpleChildWindow;assembly=MahApps.Metro.SimpleChildWindow"
Title="MahApps.Metro Simple ChildWindow Demo"
Width="700"
Height="600"
GlowBrush="{DynamicResource MahApps.Brushes.Accent}"
WindowStartupLocation="CenterScreen">

<mah:MetroWindow.RightWindowCommands>
<mah:WindowCommands>
<Button Click="Title_OnClick" Content="Test Dialog" />
</mah:WindowCommands>
</mah:MetroWindow.RightWindowCommands>

<Grid x:Name="RootGrid">

Expand Down Expand Up @@ -82,4 +88,4 @@

</Grid>

</Controls:MetroWindow>
</mah:MetroWindow>
5 changes: 5 additions & 0 deletions src/MahApps.Metro.SimpleChildWindow.Demo/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ public MainWindow()
this.InitializeComponent();
}

private async void Title_OnClick(object sender, RoutedEventArgs e)
{
await this.ShowChildWindowAsync(this.child01);
}

private void FirstTest_OnClick(object sender, RoutedEventArgs e)
{
this.child01.SetCurrentValue(ChildWindow.IsOpenProperty, true);
Expand Down
36 changes: 18 additions & 18 deletions src/MahApps.Metro.SimpleChildWindow.Demo/TestChildWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,24 @@

namespace MahApps.Metro.SimpleChildWindow.Demo
{
/// <summary>
/// Interaction logic for TestChildWindow.xaml
/// </summary>
public partial class TestChildWindow : ChildWindow
{
public TestChildWindow()
{
InitializeComponent();
}
/// <summary>
/// Interaction logic for TestChildWindow.xaml
/// </summary>
public partial class TestChildWindow : ChildWindow
{
public TestChildWindow()
{
InitializeComponent();
}

private async void MessageButtonOnClick(object sender, RoutedEventArgs e)
{
await ((MetroWindow)Window.GetWindow(this)).ShowMessageAsync("Title", "Message");
}
private async void MessageButtonOnClick(object sender, RoutedEventArgs e)
{
await ((MetroWindow)Window.GetWindow(this)).ShowMessageAsync("Title", "Message");
}

private void CloseSec_OnClick(object sender, RoutedEventArgs e)
{
this.Close();
}
}
private void CloseSec_OnClick(object sender, RoutedEventArgs e)
{
this.Close();
}
}
}
25 changes: 16 additions & 9 deletions src/MahApps.Metro.SimpleChildWindow/ChildWindowManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using MahApps.Metro.Controls;

namespace MahApps.Metro.SimpleChildWindow
{
Expand Down Expand Up @@ -138,18 +139,24 @@ public static async Task<TResult> ShowChildWindowAsync<TResult>(this Window wind

private static async Task<TResult> OpenDialogAsync<TResult>(ChildWindow dialog, Panel container, TaskCompletionSource<TResult> tcs)
{
container.Children.Add(dialog);

void OnDialogClosingFinished(object sender, RoutedEventArgs args)
if (!dialog.IsOpen)
{
dialog.ClosingFinished -= OnDialogClosingFinished;
container.Children.Remove(dialog);
tcs.TrySetResult(dialog.ChildWindowResult is TResult result ? result : (dialog.ClosedBy is TResult closedBy ? closedBy : default));
}
if (dialog.TryFindParent<Panel>() is null)
{
container.Children.Add(dialog);
}

dialog.ClosingFinished += OnDialogClosingFinished;
void OnDialogClosingFinished(object sender, RoutedEventArgs args)
{
dialog.ClosingFinished -= OnDialogClosingFinished;
container.Children.Remove(dialog);
tcs.TrySetResult(dialog.ChildWindowResult is TResult result ? result : (dialog.ClosedBy is TResult closedBy ? closedBy : default));
}

dialog.SetCurrentValue(ChildWindow.IsOpenProperty, true);
dialog.ClosingFinished += OnDialogClosingFinished;

dialog.SetCurrentValue(ChildWindow.IsOpenProperty, true);
}

return await tcs.Task;
}
Expand Down

0 comments on commit b034499

Please sign in to comment.