From f7abdbc465a4c6afbe69c074164923359f88fb62 Mon Sep 17 00:00:00 2001 From: Daniel Clark Date: Wed, 10 Feb 2021 12:11:56 +0000 Subject: [PATCH] added requested theme dp to NotificationFlyout control since it is just a normal DependencyObject --- .../NotificationFlyoutSample/Shell.xaml.cs | 24 ++++++------- .../NotificationFlyout/NotificationFlyout.cs | 36 ++++++++++++++++++- .../NotificationFlyoutPresenter.cs | 17 +-------- .../NotificationFlyoutXamlHost.cs | 32 +++++++++++++---- 4 files changed, 74 insertions(+), 35 deletions(-) diff --git a/samples/NotificationFlyoutSample/Shell.xaml.cs b/samples/NotificationFlyoutSample/Shell.xaml.cs index 1a1fbfd..127099a 100644 --- a/samples/NotificationFlyoutSample/Shell.xaml.cs +++ b/samples/NotificationFlyoutSample/Shell.xaml.cs @@ -9,18 +9,18 @@ private void Theme_SelectionChanged(object sender, Windows.UI.Xaml.Controls.SelectionChangedEventArgs e) { - //switch (Theme.SelectedIndex) - //{ - // case 0: - // RequestedTheme = Windows.UI.Xaml.ElementTheme.Default; - // break; - // case 1: - // RequestedTheme = Windows.UI.Xaml.ElementTheme.Dark; - // break; - // case 2: - // RequestedTheme = Windows.UI.Xaml.ElementTheme.Light; - // break; - //} + switch (Theme.SelectedIndex) + { + case 0: + RequestedTheme = Windows.UI.Xaml.ElementTheme.Default; + break; + case 1: + RequestedTheme = Windows.UI.Xaml.ElementTheme.Dark; + break; + case 2: + RequestedTheme = Windows.UI.Xaml.ElementTheme.Light; + break; + } } } } diff --git a/src/NotificationFlyout.Uwp.UI.Controls/NotificationFlyout/NotificationFlyout.cs b/src/NotificationFlyout.Uwp.UI.Controls/NotificationFlyout/NotificationFlyout.cs index faa4a62..6137a71 100644 --- a/src/NotificationFlyout.Uwp.UI.Controls/NotificationFlyout/NotificationFlyout.cs +++ b/src/NotificationFlyout.Uwp.UI.Controls/NotificationFlyout/NotificationFlyout.cs @@ -16,12 +16,18 @@ namespace NotificationFlyout.Uwp.UI.Controls typeof(ImageSource), typeof(NotificationFlyout), new PropertyMetadata(null, OnIconPropertyChanged)); + public static readonly DependencyProperty RequestedThemeProperty = + DependencyProperty.Register(nameof(RequestedTheme), + typeof(ElementTheme), typeof(NotificationFlyout), + new PropertyMetadata(ElementTheme.Default, OnRequestedThemePropertyChanged)); + public static DependencyProperty ContentProperty = DependencyProperty.Register(nameof(Content), typeof(UIElement), typeof(NotificationFlyout), - new PropertyMetadata(null)); + new PropertyMetadata(null, OnContentPropertyChanged)); internal event EventHandler ContentChanged; + internal event EventHandler RequestedThemeChanged; internal event EventHandler IconSourceChanged; public UIElement Content @@ -42,15 +48,43 @@ namespace NotificationFlyout.Uwp.UI.Controls set => SetValue(LightIconSourceProperty, value); } + public ElementTheme RequestedTheme + { + get => (ElementTheme)GetValue(RequestedThemeProperty); + set => SetValue(RequestedThemeProperty, value); + } + + private static void OnContentPropertyChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs args) + { + var sender = dependencyObject as NotificationFlyout; + sender?.OnContentPropertyChanged(); + } + private static void OnIconPropertyChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs args) { var sender = dependencyObject as NotificationFlyout; sender?.OnIconPropertyChanged(); } + private static void OnRequestedThemePropertyChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs args) + { + var sender = dependencyObject as NotificationFlyout; + sender?.OnRequestedThemePropertyChanged(); + } + + private void OnContentPropertyChanged() + { + ContentChanged?.Invoke(this, EventArgs.Empty); + } + private void OnIconPropertyChanged() { IconSourceChanged?.Invoke(this, EventArgs.Empty); } + + private void OnRequestedThemePropertyChanged() + { + RequestedThemeChanged?.Invoke(this, EventArgs.Empty); + } } } diff --git a/src/NotificationFlyout.Uwp.UI.Controls/NotificationFlyout/NotificationFlyoutPresenter.cs b/src/NotificationFlyout.Uwp.UI.Controls/NotificationFlyout/NotificationFlyoutPresenter.cs index 222e580..4fc489c 100644 --- a/src/NotificationFlyout.Uwp.UI.Controls/NotificationFlyout/NotificationFlyoutPresenter.cs +++ b/src/NotificationFlyout.Uwp.UI.Controls/NotificationFlyout/NotificationFlyoutPresenter.cs @@ -1,6 +1,4 @@ -using Windows.UI.Xaml; -using Windows.UI.Xaml.Controls; -using Windows.UI.Xaml.Data; +using Windows.UI.Xaml.Controls; namespace NotificationFlyout.Uwp.UI.Controls { @@ -10,18 +8,5 @@ namespace NotificationFlyout.Uwp.UI.Controls { DefaultStyleKey = typeof(NotificationFlyoutPresenter); } - - protected override void OnApplyTemplate() - { - if (GetTemplateChild("ContentPresenter") is ContentControl contentPresenter) - { - BindingOperations.SetBinding(this, RequestedThemeProperty, new Binding - { - Source = contentPresenter.Content, - Path = new PropertyPath(nameof(RequestedTheme)), - Mode = BindingMode.TwoWay - }); - } - } } } \ No newline at end of file diff --git a/src/NotificationFlyout.Wpf.UI.Controls/NotificationFlyout/NotificationFlyoutXamlHost.cs b/src/NotificationFlyout.Wpf.UI.Controls/NotificationFlyout/NotificationFlyoutXamlHost.cs index 093779d..cb764f6 100644 --- a/src/NotificationFlyout.Wpf.UI.Controls/NotificationFlyout/NotificationFlyoutXamlHost.cs +++ b/src/NotificationFlyout.Wpf.UI.Controls/NotificationFlyout/NotificationFlyoutXamlHost.cs @@ -36,19 +36,17 @@ namespace NotificationFlyout.Wpf.UI.Controls { _flyout.ContentChanged -= OnFlyoutContentChanged; _flyout.IconSourceChanged -= OnFlyoutIconSourceChanged; + _flyout.RequestedThemeChanged -= OnFlyoutIconSourceChanged; } _flyout = flyout; _flyout.ContentChanged += OnFlyoutContentChanged; _flyout.IconSourceChanged += OnFlyoutIconSourceChanged; + _flyout.RequestedThemeChanged += OnFlyoutRequestedThemeChanged; + UpdateIcons(); UpdateFlyoutContent(); - UpdateIcons(); - } - - private void OnFlyoutIconSourceChanged(object sender, EventArgs args) - { - UpdateIcons(); + UpdateRequestedTheme(); } internal void HideFlyout() @@ -91,6 +89,16 @@ namespace NotificationFlyout.Wpf.UI.Controls UpdateFlyoutContent(); } + private void OnFlyoutIconSourceChanged(object sender, EventArgs args) + { + UpdateIcons(); + } + + private void OnFlyoutRequestedThemeChanged(object sender, EventArgs args) + { + UpdateRequestedTheme(); + } + private void OnIconInvoked(object sender, NotificationIconInvokedEventArgs args) { ShowFlyout(); @@ -192,6 +200,18 @@ namespace NotificationFlyout.Wpf.UI.Controls _notificationIconHelper.SetIcon(icon.Handle); } + private void UpdateRequestedTheme() + { + if (_flyout == null) return; + + var requestedTheme = _flyout.RequestedTheme; + + var flyoutHost = GetFlyoutHost(); + if (flyoutHost != null) + { + flyoutHost.RequestedTheme = requestedTheme; + } + } private void UpdateWindow() { if (!_isLoaded) return;