From d662f066a766bfb24b84446acde353ab8c601642 Mon Sep 17 00:00:00 2001 From: Ivan Dmitriev <42055372+IvanDmitriev1@users.noreply.github.com> Date: Fri, 7 Apr 2023 12:18:03 +0600 Subject: [PATCH 1/5] Small improvements to the ContentDialog --- .../Controls/TermsOfUseContentDialog.xaml.cs | 4 +- .../ContentDialogControl/ContentDialog.cs | 46 +++++++++++-------- 2 files changed, 29 insertions(+), 21 deletions(-) diff --git a/src/Wpf.Ui.Gallery/Controls/TermsOfUseContentDialog.xaml.cs b/src/Wpf.Ui.Gallery/Controls/TermsOfUseContentDialog.xaml.cs index 8f7afc9b8..c44eedd06 100644 --- a/src/Wpf.Ui.Gallery/Controls/TermsOfUseContentDialog.xaml.cs +++ b/src/Wpf.Ui.Gallery/Controls/TermsOfUseContentDialog.xaml.cs @@ -15,11 +15,11 @@ public TermsOfUseContentDialog(ContentPresenter contentPresenter) : base(content protected override bool OnButtonClick(ContentDialogButton button) { if (CheckBox.IsChecked != false) - return true; + return false; TextBlock.Visibility = Visibility.Visible; CheckBox.Focus(); - return false; + return true; } } diff --git a/src/Wpf.Ui/Controls/ContentDialogControl/ContentDialog.cs b/src/Wpf.Ui/Controls/ContentDialogControl/ContentDialog.cs index 793ecdf41..ae84d4ec1 100644 --- a/src/Wpf.Ui/Controls/ContentDialogControl/ContentDialog.cs +++ b/src/Wpf.Ui/Controls/ContentDialogControl/ContentDialog.cs @@ -363,6 +363,12 @@ public ContentDialog(ContentPresenter contentPresenter) SetValue(TemplateButtonCommandProperty, new RelayCommand(OnTemplateButtonClick)); + + 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,34 @@ public async Task ShowAsync(CancellationToken cancellationT #else tokenRegistration.Dispose(); #endif + ContentPresenter.Content = null; + OnClosing(); } } /// - /// 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) + 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 OnClosing() + { - self.ResizeToContentSize(frameworkElement); - self.Focus(); - }; } /// @@ -460,13 +469,13 @@ protected virtual void ResizeToContentSize(UIElement content) /// /// /// - /// + /// Returns an indication of whether it has been handled or not /// - protected virtual bool OnButtonClick(ContentDialogButton button) { return true; } + protected virtual bool OnButtonClick(ContentDialogButton button) { return false; } private void OnTemplateButtonClick(ContentDialogButton button) { - if (!OnButtonClick(button)) + if (OnButtonClick(button)) return; ContentDialogResult result = button switch @@ -476,7 +485,6 @@ private void OnTemplateButtonClick(ContentDialogButton button) _ => ContentDialogResult.None }; - Hide(); - Tcs?.TrySetResult(result); + Hide(result); } } From ce7c071124f409931bb6ef4ec8770b8de9063e09 Mon Sep 17 00:00:00 2001 From: Ivan Dmitriev <42055372+IvanDmitriev1@users.noreply.github.com> Date: Fri, 7 Apr 2023 12:21:19 +0600 Subject: [PATCH 2/5] Added support for ContentTemplateSelector in the ContentDialog --- src/Wpf.Ui/Styles/Controls/ContentDialog.xaml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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}">