Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/MaterialDesignThemes.Wpf/DialogHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1022,7 +1022,7 @@ private void OnUnloaded(object sender, RoutedEventArgs routedEventArgs)
}
}

private void OnLoaded(object sender, RoutedEventArgs routedEventArgs)
private void Register()
{
foreach (var weakRef in LoadedInstances.ToList())
{
Expand All @@ -1033,6 +1033,11 @@ private void OnLoaded(object sender, RoutedEventArgs routedEventArgs)
}

LoadedInstances.Add(new WeakReference<DialogHost>(this));
}

private void OnLoaded(object sender, RoutedEventArgs routedEventArgs)
{
Register();

if (IsOpen && _popup is { } popup)
{
Expand All @@ -1044,6 +1049,12 @@ private void OnLoaded(object sender, RoutedEventArgs routedEventArgs)
}
}

protected override void OnInitialized(EventArgs e)
{
base.OnInitialized(e);
Register();
}

[SecurityCritical]
[DllImport("user32.dll", EntryPoint = "SetFocus", SetLastError = true)]
private static extern IntPtr SetFocus(IntPtr hWnd);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Window x:Class="MaterialDesignThemes.UITests.Samples.DialogHost.OpenDialogInConstructorWindow"
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:local="clr-namespace:MaterialDesignThemes.UITests.Samples.DialogHost"
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Title="OpenDialogInConstructorWindow"
Width="800"
Height="450"
mc:Ignorable="d">
<materialDesign:DialogHost />
</Window>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace MaterialDesignThemes.UITests.Samples.DialogHost;

/// <summary>
/// Interaction logic for OpenDialogInConstructorWindow.xaml
/// </summary>
public partial class OpenDialogInConstructorWindow : Window
{
public OpenDialogInConstructorWindow()
{
InitializeComponent();
MaterialDesignThemes.Wpf.DialogHost.Show("Dialog opened in constructor");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,18 @@ public async Task DialogHost_FocusMovedInsideDialogWhileOpening_IsKept(string di
}
}

[Test]
[DoesNotRequireInteraction]
[Description("Issue 3804")]
public async Task DialogHost_CanOpenDialog_FromWindowConstructor()
{
await App.InitializeWithMaterialDesign();
IWindow window = await App.CreateWindow<OpenDialogInConstructorWindow>();
var dialogHost = await window.GetElement<DialogHost>();

await Assert.That(await dialogHost.GetIsOpen()).IsTrue();
}

private static object SetDialogHostStyle(DialogHost dialogHost, string styleName)
{
var style = (Style)dialogHost.FindResource(styleName);
Expand Down
Loading