NotifiIcon的ContextMenu -> MenuItem -> Command 可以使用绑定
This commit is contained in:
1 parent
1abbedcbe0
commit
a53f495d7d
3 files changed
+45
-1
No files matched your search
@@ -41,6 +41,7 @@
|
|||||||
<hc:NotifyIcon Token="{x:Static data:MessageToken.NotifyIconDemo}" Text="HandyControl" IsBlink="{Binding ContextMenuIsBlink}" Visibility="{Binding ContextMenuIsShow,Converter={StaticResource Boolean2VisibilityConverter}}">
|
<hc:NotifyIcon Token="{x:Static data:MessageToken.NotifyIconDemo}" Text="HandyControl" IsBlink="{Binding ContextMenuIsBlink}" Visibility="{Binding ContextMenuIsShow,Converter={StaticResource Boolean2VisibilityConverter}}">
|
||||||
<hc:NotifyIcon.ContextMenu>
|
<hc:NotifyIcon.ContextMenu>
|
||||||
<ContextMenu>
|
<ContextMenu>
|
||||||
|
<MenuItem Command="{Binding SendNotificationCmd}" Header="{x:Static langs:Lang.SendNotification}"/>
|
||||||
<MenuItem Command="hc:ControlCommands.PushMainWindow2Top" Header="{x:Static langs:Lang.OpenPanel}"/>
|
<MenuItem Command="hc:ControlCommands.PushMainWindow2Top" Header="{x:Static langs:Lang.OpenPanel}"/>
|
||||||
<MenuItem Command="hc:ControlCommands.ShutdownApp" Header="{x:Static langs:Lang.Exit}"/>
|
<MenuItem Command="hc:ControlCommands.ShutdownApp" Header="{x:Static langs:Lang.Exit}"/>
|
||||||
</ContextMenu>
|
</ContextMenu>
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ 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
|
||||||
@@ -62,6 +63,8 @@ namespace HandyControl.Controls
|
|||||||
static NotifyIcon()
|
static NotifyIcon()
|
||||||
{
|
{
|
||||||
VisibilityProperty.OverrideMetadata(typeof(NotifyIcon), new PropertyMetadata(Visibility.Visible, OnVisibilityChanged));
|
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)
|
private static void OnVisibilityChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
|
||||||
@@ -82,6 +85,30 @@ namespace HandyControl.Controls
|
|||||||
ctl.UpdateIcon(false);
|
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()
|
public NotifyIcon()
|
||||||
{
|
{
|
||||||
@@ -676,6 +703,15 @@ namespace HandyControl.Controls
|
|||||||
remove => RemoveHandler(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)
|
private void Dispose(bool disposing)
|
||||||
{
|
{
|
||||||
if (_isDisposed) return;
|
if (_isDisposed) return;
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
using System.Windows;
|
using System;
|
||||||
|
using System.Windows;
|
||||||
|
|
||||||
namespace HandyControl.Tools.Extension
|
namespace HandyControl.Tools.Extension
|
||||||
{
|
{
|
||||||
@@ -54,5 +55,11 @@ 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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user