From a53f495d7dbc834bfbcc6a3f0c497727b1e572a3 Mon Sep 17 00:00:00 2001 From: yxhq Date: Fri, 13 Mar 2020 21:27:42 +0800 Subject: [PATCH 1/2] =?UTF-8?q?NotifiIcon=E7=9A=84ContextMenu=20->=20MenuI?= =?UTF-8?q?tem=20->=20Command=20=E5=8F=AF=E4=BB=A5=E4=BD=BF=E7=94=A8?= =?UTF-8?q?=E7=BB=91=E5=AE=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controls/NotifyIconDemoCtl.xaml | 1 + .../Controls/Other/NotifyIcon.cs | 54 +++++++++++++++---- .../Extension/FrameworkElementExtension.cs | 9 +++- 3 files changed, 54 insertions(+), 10 deletions(-) diff --git a/src/Shared/HandyControlDemo_Shared/UserControl/Controls/NotifyIconDemoCtl.xaml b/src/Shared/HandyControlDemo_Shared/UserControl/Controls/NotifyIconDemoCtl.xaml index 36c6aeac..40f5a071 100644 --- a/src/Shared/HandyControlDemo_Shared/UserControl/Controls/NotifyIconDemoCtl.xaml +++ b/src/Shared/HandyControlDemo_Shared/UserControl/Controls/NotifyIconDemoCtl.xaml @@ -41,6 +41,7 @@ + diff --git a/src/Shared/HandyControl_Shared/Controls/Other/NotifyIcon.cs b/src/Shared/HandyControl_Shared/Controls/Other/NotifyIcon.cs index 6531ccf6..5dff7b65 100644 --- a/src/Shared/HandyControl_Shared/Controls/Other/NotifyIcon.cs +++ b/src/Shared/HandyControl_Shared/Controls/Other/NotifyIcon.cs @@ -11,6 +11,7 @@ using System.Windows.Media; using System.Windows.Threading; using HandyControl.Data; using HandyControl.Tools; +using HandyControl.Tools.Extension; using HandyControl.Tools.Interop; namespace HandyControl.Controls @@ -62,11 +63,13 @@ namespace HandyControl.Controls static NotifyIcon() { VisibilityProperty.OverrideMetadata(typeof(NotifyIcon), new PropertyMetadata(Visibility.Visible, OnVisibilityChanged)); + DataContextProperty.OverrideMetadata(typeof(NotifyIcon), new FrameworkPropertyMetadata(DataContextPropertyChanged)); + ContextMenuProperty.OverrideMetadata(typeof(NotifyIcon), new FrameworkPropertyMetadata(ContextMenuPropertyChanged)); } private static void OnVisibilityChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { - var ctl = (NotifyIcon) d; + var ctl = (NotifyIcon)d; var v = (Visibility)e.NewValue; if (v == Visibility.Visible) @@ -77,11 +80,35 @@ namespace HandyControl.Controls } ctl.UpdateIcon(true); } - else if(ctl._iconCurrentHandle != IntPtr.Zero) + else if (ctl._iconCurrentHandle != IntPtr.Zero) { ctl.UpdateIcon(false); } } + private static void DataContextPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) + { + NotifyIcon owner = (NotifyIcon)d; + owner.OnDataContextPropertyChanged(e); + } + + private void OnDataContextPropertyChanged(DependencyPropertyChangedEventArgs e) + { + object newValue = e.NewValue; + object oldValue = e.OldValue; + UpdateDataContext(_contextContent, oldValue, newValue); + UpdateDataContext(ContextMenu, oldValue, newValue); + } + + private static void ContextMenuPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) + { + NotifyIcon owner = (NotifyIcon)d; + owner.OnContextMenuPropertyChanged(e); + } + + private void OnContextMenuPropertyChanged(DependencyPropertyChangedEventArgs e) + { + UpdateDataContext((ContextMenu)e.NewValue, null, DataContext); + } public NotifyIcon() { @@ -228,7 +255,7 @@ namespace HandyControl.Controls public string Token { - get => (string) GetValue(TokenProperty); + get => (string)GetValue(TokenProperty); set => SetValue(TokenProperty, value); } @@ -237,7 +264,7 @@ namespace HandyControl.Controls public string Text { - get => (string) GetValue(TextProperty); + get => (string)GetValue(TextProperty); set => SetValue(TextProperty, value); } @@ -274,13 +301,13 @@ namespace HandyControl.Controls var ctl = (NotifyIcon)d; if (ctl._dispatcherTimerBlink != null) { - ctl._dispatcherTimerBlink.Interval = (TimeSpan) e.NewValue; + ctl._dispatcherTimerBlink.Interval = (TimeSpan)e.NewValue; } } public TimeSpan BlinkInterval { - get => (TimeSpan) GetValue(BlinkIntervalProperty); + get => (TimeSpan)GetValue(BlinkIntervalProperty); set => SetValue(BlinkIntervalProperty, value); } @@ -291,7 +318,7 @@ namespace HandyControl.Controls { var ctl = (NotifyIcon)d; if (ctl.Visibility != Visibility.Visible) return; - if ((bool) e.NewValue) + if ((bool)e.NewValue) { if (ctl._dispatcherTimerBlink == null) { @@ -465,7 +492,7 @@ namespace HandyControl.Controls InteropMethods.VirtualFreeEx(hProcess, address, 0, InteropValues.FreeType.Release); InteropMethods.CloseHandle(hProcess); } - return isFind; + return isFind; } private void OnIconChanged() @@ -674,7 +701,16 @@ namespace HandyControl.Controls { add => AddHandler(MouseDoubleClickEvent, value); remove => RemoveHandler(MouseDoubleClickEvent, value); - } + } + + private void UpdateDataContext(FrameworkElement target, object oldDataContextValue, object newDataContextValue) + { + if (target == null || target.IsDataContextDataBound()) return; + if (ReferenceEquals(this, target.DataContext) || Equals(oldDataContextValue, target.DataContext)) + { + target.DataContext = newDataContextValue ?? this; + } + } private void Dispose(bool disposing) { diff --git a/src/Shared/HandyControl_Shared/Tools/Extension/FrameworkElementExtension.cs b/src/Shared/HandyControl_Shared/Tools/Extension/FrameworkElementExtension.cs index 8810122d..8028f5b8 100644 --- a/src/Shared/HandyControl_Shared/Tools/Extension/FrameworkElementExtension.cs +++ b/src/Shared/HandyControl_Shared/Tools/Extension/FrameworkElementExtension.cs @@ -1,4 +1,5 @@ -using System.Windows; +using System; +using System.Windows; namespace HandyControl.Tools.Extension { @@ -54,5 +55,11 @@ namespace HandyControl.Tools.Extension return 0; } + + public static bool IsDataContextDataBound(this FrameworkElement element) + { + if (element == null) throw new ArgumentNullException(nameof(element)); + return element.GetBindingExpression(FrameworkElement.DataContextProperty) != null; + } } } \ No newline at end of file From e5307d0b4cf4878921dc539978dd36b5c1a795d0 Mon Sep 17 00:00:00 2001 From: NaBian <836904362@qq.com> Date: Sat, 4 Apr 2020 00:09:49 +0800 Subject: [PATCH 2/2] update pr/338 --- .../Controls/Other/NotifyIcon.cs | 34 ++++++++----------- .../Extension/FrameworkElementExtension.cs | 9 +---- 2 files changed, 15 insertions(+), 28 deletions(-) diff --git a/src/Shared/HandyControl_Shared/Controls/Other/NotifyIcon.cs b/src/Shared/HandyControl_Shared/Controls/Other/NotifyIcon.cs index 5dff7b65..592f3313 100644 --- a/src/Shared/HandyControl_Shared/Controls/Other/NotifyIcon.cs +++ b/src/Shared/HandyControl_Shared/Controls/Other/NotifyIcon.cs @@ -6,12 +6,12 @@ using System.Runtime.InteropServices; using System.Windows; using System.Windows.Controls; using System.Windows.Controls.Primitives; +using System.Windows.Data; using System.Windows.Input; using System.Windows.Media; using System.Windows.Threading; using HandyControl.Data; using HandyControl.Tools; -using HandyControl.Tools.Extension; using HandyControl.Tools.Interop; namespace HandyControl.Controls @@ -85,30 +85,24 @@ namespace HandyControl.Controls ctl.UpdateIcon(false); } } - private static void DataContextPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) - { - NotifyIcon owner = (NotifyIcon)d; - owner.OnDataContextPropertyChanged(e); - } + + private static void DataContextPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) => + ((NotifyIcon) d).OnDataContextPropertyChanged(e); private void OnDataContextPropertyChanged(DependencyPropertyChangedEventArgs e) { - object newValue = e.NewValue; - object oldValue = e.OldValue; - UpdateDataContext(_contextContent, oldValue, newValue); - UpdateDataContext(ContextMenu, oldValue, newValue); + UpdateDataContext(_contextContent, e.OldValue, e.NewValue); + UpdateDataContext(ContextMenu, e.OldValue, e.NewValue); } private static void ContextMenuPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { - NotifyIcon owner = (NotifyIcon)d; - owner.OnContextMenuPropertyChanged(e); + var ctl = (NotifyIcon)d; + ctl.OnContextMenuPropertyChanged(e); } - private void OnContextMenuPropertyChanged(DependencyPropertyChangedEventArgs e) - { - UpdateDataContext((ContextMenu)e.NewValue, null, DataContext); - } + private void OnContextMenuPropertyChanged(DependencyPropertyChangedEventArgs e) => + UpdateDataContext((ContextMenu) e.NewValue, null, DataContext); public NotifyIcon() { @@ -703,12 +697,12 @@ namespace HandyControl.Controls remove => RemoveHandler(MouseDoubleClickEvent, value); } - private void UpdateDataContext(FrameworkElement target, object oldDataContextValue, object newDataContextValue) + private void UpdateDataContext(FrameworkElement target, object oldValue, object newValue) { - if (target == null || target.IsDataContextDataBound()) return; - if (ReferenceEquals(this, target.DataContext) || Equals(oldDataContextValue, target.DataContext)) + if (target == null || BindingOperations.GetBindingExpression(target, DataContextProperty) != null) return; + if (ReferenceEquals(this, target.DataContext) || Equals(oldValue, target.DataContext)) { - target.DataContext = newDataContextValue ?? this; + target.DataContext = newValue ?? this; } } diff --git a/src/Shared/HandyControl_Shared/Tools/Extension/FrameworkElementExtension.cs b/src/Shared/HandyControl_Shared/Tools/Extension/FrameworkElementExtension.cs index 8028f5b8..8810122d 100644 --- a/src/Shared/HandyControl_Shared/Tools/Extension/FrameworkElementExtension.cs +++ b/src/Shared/HandyControl_Shared/Tools/Extension/FrameworkElementExtension.cs @@ -1,5 +1,4 @@ -using System; -using System.Windows; +using System.Windows; namespace HandyControl.Tools.Extension { @@ -55,11 +54,5 @@ namespace HandyControl.Tools.Extension return 0; } - - public static bool IsDataContextDataBound(this FrameworkElement element) - { - if (element == null) throw new ArgumentNullException(nameof(element)); - return element.GetBindingExpression(FrameworkElement.DataContextProperty) != null; - } } } \ No newline at end of file