diff --git a/src/Wpf.Ui.Gallery/Controls/TermsOfUseContentDialog.xaml.cs b/src/Wpf.Ui.Gallery/Controls/TermsOfUseContentDialog.xaml.cs index 8f7afc9b8..8e595e94d 100644 --- a/src/Wpf.Ui.Gallery/Controls/TermsOfUseContentDialog.xaml.cs +++ b/src/Wpf.Ui.Gallery/Controls/TermsOfUseContentDialog.xaml.cs @@ -1,6 +1,5 @@ using System.Windows; using System.Windows.Controls; -using Wpf.Ui.Controls; using Wpf.Ui.Controls.ContentDialogControl; namespace Wpf.Ui.Gallery.Controls; @@ -12,14 +11,15 @@ public TermsOfUseContentDialog(ContentPresenter contentPresenter) : base(content InitializeComponent(); } - protected override bool OnButtonClick(ContentDialogButton button) + protected override void OnButtonClick(ContentDialogButton button) { if (CheckBox.IsChecked != false) - return true; + { + base.OnButtonClick(button); + return; + }; TextBlock.Visibility = Visibility.Visible; CheckBox.Focus(); - - return false; } } diff --git a/src/Wpf.Ui/Controls/ContentDialogControl/ContentDialog.cs b/src/Wpf.Ui/Controls/ContentDialogControl/ContentDialog.cs index 793ecdf41..8f2974ea9 100644 --- a/src/Wpf.Ui/Controls/ContentDialogControl/ContentDialog.cs +++ b/src/Wpf.Ui/Controls/ContentDialogControl/ContentDialog.cs @@ -362,7 +362,13 @@ public ContentDialog(ContentPresenter contentPresenter) ContentPresenter = contentPresenter; SetValue(TemplateButtonCommandProperty, - new RelayCommand(OnTemplateButtonClick)); + new RelayCommand(OnButtonClick)); + + Loaded += static (sender, _) => + { + var self = (ContentDialog)sender; + self.OnLoaded(); + }; } protected readonly ContentPresenter ContentPresenter; @@ -377,7 +383,7 @@ public ContentDialog(ContentPresenter contentPresenter) public async Task ShowAsync(CancellationToken cancellationToken = default) { Tcs = new TaskCompletionSource(); - var tokenRegistration = cancellationToken.Register(o => Tcs.TrySetCanceled((CancellationToken)o!), cancellationToken); + CancellationTokenRegistration tokenRegistration = cancellationToken.Register(o => Tcs.TrySetCanceled((CancellationToken)o!), cancellationToken); try { @@ -391,31 +397,37 @@ public async Task ShowAsync(CancellationToken cancellationT #else tokenRegistration.Dispose(); #endif + ContentPresenter.Content = null; + OnClosed(); } } /// - /// Hides the dialog + /// Hides the dialog with result /// - public virtual void Hide() + public virtual void Hide(ContentDialogResult result = ContentDialogResult.None) { - ContentPresenter.Content = null; + Tcs?.TrySetResult(result); } - protected override void OnInitialized(EventArgs e) + /// + /// Occurs after Loading event + /// + protected virtual void OnLoaded() { - base.OnInitialized(e); + if (VisualChildrenCount <= 0 || GetVisualChild(0) is not UIElement frameworkElement) + return; - Loaded += static (sender, _) => - { - var self = (ContentDialog)sender; + ResizeToContentSize(frameworkElement); + Focus(); + } - if (self.VisualChildrenCount <= 0 || self.GetVisualChild(0) is not UIElement frameworkElement) - return; + /// + /// Occurs after ContentPresenter.Content = null + /// + protected virtual void OnClosed() + { - self.ResizeToContentSize(frameworkElement); - self.Focus(); - }; } /// @@ -459,16 +471,8 @@ protected virtual void ResizeToContentSize(UIElement content) /// Occurs after the is clicked /// /// - /// - /// - /// - protected virtual bool OnButtonClick(ContentDialogButton button) { return true; } - - private void OnTemplateButtonClick(ContentDialogButton button) + protected virtual void OnButtonClick(ContentDialogButton button) { - if (!OnButtonClick(button)) - return; - ContentDialogResult result = button switch { ContentDialogButton.Primary => ContentDialogResult.Primary, @@ -476,7 +480,6 @@ private void OnTemplateButtonClick(ContentDialogButton button) _ => ContentDialogResult.None }; - Hide(); - Tcs?.TrySetResult(result); + Hide(result); } } diff --git a/src/Wpf.Ui/Controls/IconElements/IconSourceElement.cs b/src/Wpf.Ui/Controls/IconElements/IconSourceElement.cs index 2dd671367..f051e24b5 100644 --- a/src/Wpf.Ui/Controls/IconElements/IconSourceElement.cs +++ b/src/Wpf.Ui/Controls/IconElements/IconSourceElement.cs @@ -38,6 +38,7 @@ public IconSource? IconSource protected override UIElement InitializeChildren() { + //TODO come up with an elegant solution throw new InvalidOperationException($"Use {nameof(IconSourceElementConverter)} class."); } } diff --git a/src/Wpf.Ui/Controls/MessageBoxControl/MessageBox.cs b/src/Wpf.Ui/Controls/MessageBoxControl/MessageBox.cs index ce0d0b585..f2b03d274 100644 --- a/src/Wpf.Ui/Controls/MessageBoxControl/MessageBox.cs +++ b/src/Wpf.Ui/Controls/MessageBoxControl/MessageBox.cs @@ -245,7 +245,15 @@ public bool IsPrimaryButtonEnabled public MessageBox() { Topmost = true; - SetValue(TemplateButtonCommandProperty, new RelayCommand(OnTemplateButtonClick)); + SetValue(TemplateButtonCommandProperty, new RelayCommand(OnButtonClick)); + + PreviewMouseDoubleClick += static (_, args) => args.Handled = true; + + Loaded += static (sender, _) => + { + var self = (MessageBox)sender; + self.OnLoaded(); + }; } protected TaskCompletionSource? Tcs; @@ -259,8 +267,13 @@ public MessageBox() [Obsolete($"Use {nameof(ShowDialogAsync)} instead")] public new bool? ShowDialog() { - RemoveTitleBarAndApplyMica(); - return base.ShowDialog(); + throw new InvalidOperationException($"Use {nameof(ShowDialogAsync)} instead"); + } + + [Obsolete($"Use {nameof(Close)} with MessageBoxResult instead")] + public new void Close() + { + throw new InvalidOperationException($"Use {nameof(Close)} with MessageBoxResult instead"); } /// @@ -268,18 +281,21 @@ public MessageBox() /// /// /// - public async Task ShowDialogAsync(CancellationToken cancellationToken = default) + public async Task ShowDialogAsync(bool showAsDialog = true, CancellationToken cancellationToken = default) { Tcs = new TaskCompletionSource(); - var tokenRegistration = cancellationToken.Register(o => Tcs.TrySetCanceled((CancellationToken)o!), cancellationToken); + CancellationTokenRegistration tokenRegistration = cancellationToken.Register(o => Tcs.TrySetCanceled((CancellationToken)o!), cancellationToken); try { -#pragma warning disable CS0618 - ShowDialog(); -#pragma warning restore CS0618 + RemoveTitleBarAndApplyMica(); + + if (showAsDialog) + base.ShowDialog(); + else + base.Show(); - return await Tcs!.Task; + return await Tcs.Task; } finally { @@ -291,22 +307,16 @@ public async Task ShowDialogAsync(CancellationToken cancellati } } - protected override void OnInitialized(EventArgs e) + /// + /// Occurs after Loading event + /// + protected virtual void OnLoaded() { - base.OnInitialized(e); - - PreviewMouseDoubleClick += static (_, args) => args.Handled = true; - - Loaded += static (sender, _) => - { - var self = (MessageBox)sender; - - if (self.VisualChildrenCount <= 0 || self.GetVisualChild(0) is not UIElement content) - return; + if (VisualChildrenCount <= 0 || GetVisualChild(0) is not UIElement content) + return; - self.ResizeToContentSize(content); - self.CenterWindowOnScreen(); - }; + ResizeToContentSize(content); + CenterWindowOnScreen(); } protected override void OnClosing(CancelEventArgs e) @@ -369,22 +379,8 @@ protected virtual void CenterWindowOnScreen() /// Occurs after the is clicked /// /// - /// - /// - /// - protected virtual bool OnButtonClick(MessageBoxButton button) { return true; } - - private void RemoveTitleBarAndApplyMica() + protected virtual void OnButtonClick(MessageBoxButton button) { - UnsafeNativeMethods.RemoveWindowTitlebarContents(this); - WindowBackdrop.ApplyBackdrop(this, WindowBackdropType.Mica); - } - - private void OnTemplateButtonClick(MessageBoxButton button) - { - if (!OnButtonClick(button)) - return; - MessageBoxResult result = button switch { MessageBoxButton.Primary => MessageBoxResult.Primary, @@ -393,6 +389,12 @@ private void OnTemplateButtonClick(MessageBoxButton button) }; Tcs?.TrySetResult(result); - Close(); + base.Close(); + } + + private void RemoveTitleBarAndApplyMica() + { + UnsafeNativeMethods.RemoveWindowTitlebarContents(this); + WindowBackdrop.ApplyBackdrop(this, WindowBackdropType.Mica); } } diff --git a/src/Wpf.Ui/Styles/Controls/ContentDialog.xaml b/src/Wpf.Ui/Styles/Controls/ContentDialog.xaml index 0cceaf774..db0d70a6b 100644 --- a/src/Wpf.Ui/Styles/Controls/ContentDialog.xaml +++ b/src/Wpf.Ui/Styles/Controls/ContentDialog.xaml @@ -92,7 +92,8 @@ HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" Content="{TemplateBinding Content}" - ContentTemplate="{TemplateBinding ContentTemplate}"> + ContentTemplate="{TemplateBinding ContentTemplate}" + ContentTemplateSelector="{TemplateBinding ContentTemplateSelector}">