update pr/338

This commit is contained in:
NaBian committed 2020-04-04 00:09:49 +08:00
1 parent 7ff1ed6fed
commit e5307d0b4c
2 files changed
+15 -28

No files matched your search

@@ -6,12 +6,12 @@ using System.Runtime.InteropServices;
using System.Windows; using System.Windows;
using System.Windows.Controls; using System.Windows.Controls;
using System.Windows.Controls.Primitives; using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Input; using System.Windows.Input;
using System.Windows.Media; using System.Windows.Media;
using System.Windows.Threading; using System.Windows.Threading;
using HandyControl.Data; using HandyControl.Data;
using HandyControl.Tools; using HandyControl.Tools;
using HandyControl.Tools.Extension;
using HandyControl.Tools.Interop; using HandyControl.Tools.Interop;
namespace HandyControl.Controls namespace HandyControl.Controls
@@ -85,30 +85,24 @@ namespace HandyControl.Controls
ctl.UpdateIcon(false); ctl.UpdateIcon(false);
} }
} }
private static void DataContextPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{ private static void DataContextPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) =>
NotifyIcon owner = (NotifyIcon)d; ((NotifyIcon) d).OnDataContextPropertyChanged(e);
owner.OnDataContextPropertyChanged(e);
}
private void OnDataContextPropertyChanged(DependencyPropertyChangedEventArgs e) private void OnDataContextPropertyChanged(DependencyPropertyChangedEventArgs e)
{ {
object newValue = e.NewValue; UpdateDataContext(_contextContent, e.OldValue, e.NewValue);
object oldValue = e.OldValue; UpdateDataContext(ContextMenu, e.OldValue, e.NewValue);
UpdateDataContext(_contextContent, oldValue, newValue);
UpdateDataContext(ContextMenu, oldValue, newValue);
} }
private static void ContextMenuPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) private static void ContextMenuPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{ {
NotifyIcon owner = (NotifyIcon)d; var ctl = (NotifyIcon)d;
owner.OnContextMenuPropertyChanged(e); ctl.OnContextMenuPropertyChanged(e);
} }
private void OnContextMenuPropertyChanged(DependencyPropertyChangedEventArgs e) private void OnContextMenuPropertyChanged(DependencyPropertyChangedEventArgs e) =>
{ UpdateDataContext((ContextMenu) e.NewValue, null, DataContext);
UpdateDataContext((ContextMenu)e.NewValue, null, DataContext);
}
public NotifyIcon() public NotifyIcon()
{ {
@@ -703,12 +697,12 @@ namespace HandyControl.Controls
remove => RemoveHandler(MouseDoubleClickEvent, value); 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 (target == null || BindingOperations.GetBindingExpression(target, DataContextProperty) != null) return;
if (ReferenceEquals(this, target.DataContext) || Equals(oldDataContextValue, target.DataContext)) if (ReferenceEquals(this, target.DataContext) || Equals(oldValue, target.DataContext))
{ {
target.DataContext = newDataContextValue ?? this; target.DataContext = newValue ?? this;
} }
} }
@@ -1,5 +1,4 @@
using System; using System.Windows;
using System.Windows;
namespace HandyControl.Tools.Extension namespace HandyControl.Tools.Extension
{ {
@@ -55,11 +54,5 @@ namespace HandyControl.Tools.Extension
return 0; return 0;
} }
public static bool IsDataContextDataBound(this FrameworkElement element)
{
if (element == null) throw new ArgumentNullException(nameof(element));
return element.GetBindingExpression(FrameworkElement.DataContextProperty) != null;
}
} }
} }