mirror of
https://gitee.com/handyorg/HandyControl.git
synced 2024-12-05 13:28:18 +08:00
commit
15bf905515
@ -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>
|
||||||
|
@ -6,6 +6,7 @@ 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;
|
||||||
@ -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)
|
||||||
@ -83,6 +86,24 @@ namespace HandyControl.Controls
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static void DataContextPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) =>
|
||||||
|
((NotifyIcon) d).OnDataContextPropertyChanged(e);
|
||||||
|
|
||||||
|
private void OnDataContextPropertyChanged(DependencyPropertyChangedEventArgs e)
|
||||||
|
{
|
||||||
|
UpdateDataContext(_contextContent, e.OldValue, e.NewValue);
|
||||||
|
UpdateDataContext(ContextMenu, e.OldValue, e.NewValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void ContextMenuPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
|
||||||
|
{
|
||||||
|
var ctl = (NotifyIcon)d;
|
||||||
|
ctl.OnContextMenuPropertyChanged(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void OnContextMenuPropertyChanged(DependencyPropertyChangedEventArgs e) =>
|
||||||
|
UpdateDataContext((ContextMenu) e.NewValue, null, DataContext);
|
||||||
|
|
||||||
public NotifyIcon()
|
public NotifyIcon()
|
||||||
{
|
{
|
||||||
_id = ++NextId;
|
_id = ++NextId;
|
||||||
@ -676,6 +697,15 @@ namespace HandyControl.Controls
|
|||||||
remove => RemoveHandler(MouseDoubleClickEvent, value);
|
remove => RemoveHandler(MouseDoubleClickEvent, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void UpdateDataContext(FrameworkElement target, object oldValue, object newValue)
|
||||||
|
{
|
||||||
|
if (target == null || BindingOperations.GetBindingExpression(target, DataContextProperty) != null) return;
|
||||||
|
if (ReferenceEquals(this, target.DataContext) || Equals(oldValue, target.DataContext))
|
||||||
|
{
|
||||||
|
target.DataContext = newValue ?? this;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void Dispose(bool disposing)
|
private void Dispose(bool disposing)
|
||||||
{
|
{
|
||||||
if (_isDisposed) return;
|
if (_isDisposed) return;
|
||||||
|
Loading…
Reference in New Issue
Block a user