added requested theme dp to NotificationFlyout control since it is just a normal DependencyObject

This commit is contained in:
Daniel Clark
2021-02-10 12:11:56 +00:00
parent b5b8641527
commit f7abdbc465
4 changed files with 74 additions and 35 deletions
+12 -12
View File
@@ -9,18 +9,18 @@
private void Theme_SelectionChanged(object sender, Windows.UI.Xaml.Controls.SelectionChangedEventArgs e) private void Theme_SelectionChanged(object sender, Windows.UI.Xaml.Controls.SelectionChangedEventArgs e)
{ {
//switch (Theme.SelectedIndex) switch (Theme.SelectedIndex)
//{ {
// case 0: case 0:
// RequestedTheme = Windows.UI.Xaml.ElementTheme.Default; RequestedTheme = Windows.UI.Xaml.ElementTheme.Default;
// break; break;
// case 1: case 1:
// RequestedTheme = Windows.UI.Xaml.ElementTheme.Dark; RequestedTheme = Windows.UI.Xaml.ElementTheme.Dark;
// break; break;
// case 2: case 2:
// RequestedTheme = Windows.UI.Xaml.ElementTheme.Light; RequestedTheme = Windows.UI.Xaml.ElementTheme.Light;
// break; break;
//} }
} }
} }
} }
@@ -16,12 +16,18 @@ namespace NotificationFlyout.Uwp.UI.Controls
typeof(ImageSource), typeof(NotificationFlyout), typeof(ImageSource), typeof(NotificationFlyout),
new PropertyMetadata(null, OnIconPropertyChanged)); 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 = public static DependencyProperty ContentProperty =
DependencyProperty.Register(nameof(Content), DependencyProperty.Register(nameof(Content),
typeof(UIElement), typeof(NotificationFlyout), typeof(UIElement), typeof(NotificationFlyout),
new PropertyMetadata(null)); new PropertyMetadata(null, OnContentPropertyChanged));
internal event EventHandler ContentChanged; internal event EventHandler ContentChanged;
internal event EventHandler RequestedThemeChanged;
internal event EventHandler IconSourceChanged; internal event EventHandler IconSourceChanged;
public UIElement Content public UIElement Content
@@ -42,15 +48,43 @@ namespace NotificationFlyout.Uwp.UI.Controls
set => SetValue(LightIconSourceProperty, value); 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) private static void OnIconPropertyChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs args)
{ {
var sender = dependencyObject as NotificationFlyout; var sender = dependencyObject as NotificationFlyout;
sender?.OnIconPropertyChanged(); 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() private void OnIconPropertyChanged()
{ {
IconSourceChanged?.Invoke(this, EventArgs.Empty); IconSourceChanged?.Invoke(this, EventArgs.Empty);
} }
private void OnRequestedThemePropertyChanged()
{
RequestedThemeChanged?.Invoke(this, EventArgs.Empty);
}
} }
} }
@@ -1,6 +1,4 @@
using Windows.UI.Xaml; using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Data;
namespace NotificationFlyout.Uwp.UI.Controls namespace NotificationFlyout.Uwp.UI.Controls
{ {
@@ -10,18 +8,5 @@ namespace NotificationFlyout.Uwp.UI.Controls
{ {
DefaultStyleKey = typeof(NotificationFlyoutPresenter); 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
});
}
}
} }
} }
@@ -36,19 +36,17 @@ namespace NotificationFlyout.Wpf.UI.Controls
{ {
_flyout.ContentChanged -= OnFlyoutContentChanged; _flyout.ContentChanged -= OnFlyoutContentChanged;
_flyout.IconSourceChanged -= OnFlyoutIconSourceChanged; _flyout.IconSourceChanged -= OnFlyoutIconSourceChanged;
_flyout.RequestedThemeChanged -= OnFlyoutIconSourceChanged;
} }
_flyout = flyout; _flyout = flyout;
_flyout.ContentChanged += OnFlyoutContentChanged; _flyout.ContentChanged += OnFlyoutContentChanged;
_flyout.IconSourceChanged += OnFlyoutIconSourceChanged; _flyout.IconSourceChanged += OnFlyoutIconSourceChanged;
_flyout.RequestedThemeChanged += OnFlyoutRequestedThemeChanged;
UpdateIcons();
UpdateFlyoutContent(); UpdateFlyoutContent();
UpdateIcons(); UpdateRequestedTheme();
}
private void OnFlyoutIconSourceChanged(object sender, EventArgs args)
{
UpdateIcons();
} }
internal void HideFlyout() internal void HideFlyout()
@@ -91,6 +89,16 @@ namespace NotificationFlyout.Wpf.UI.Controls
UpdateFlyoutContent(); UpdateFlyoutContent();
} }
private void OnFlyoutIconSourceChanged(object sender, EventArgs args)
{
UpdateIcons();
}
private void OnFlyoutRequestedThemeChanged(object sender, EventArgs args)
{
UpdateRequestedTheme();
}
private void OnIconInvoked(object sender, NotificationIconInvokedEventArgs args) private void OnIconInvoked(object sender, NotificationIconInvokedEventArgs args)
{ {
ShowFlyout(); ShowFlyout();
@@ -192,6 +200,18 @@ namespace NotificationFlyout.Wpf.UI.Controls
_notificationIconHelper.SetIcon(icon.Handle); _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() private void UpdateWindow()
{ {
if (!_isLoaded) return; if (!_isLoaded) return;