From 6e1d294202ad5925e5289c552c9c444a71c315b4 Mon Sep 17 00:00:00 2001 From: NaBian <836904362@qq.com> Date: Wed, 18 Sep 2019 21:49:37 +0800 Subject: [PATCH] fixed #171 --- .../UserControl/Controls/SideMenuDemoCtl.xaml | 6 +- .../Controls/SideMenuDemoViewModel.cs | 5 ++ .../Controls/Input/SearchBar.cs | 16 +++-- .../Controls/SideMenu/SideMenuItem.cs | 71 ++++++++++++++++++- .../Controls/Window/BlurWindow.cs | 22 +++--- .../Data/SystemVersionInfo.cs | 3 + 6 files changed, 97 insertions(+), 26 deletions(-) diff --git a/src/Shared/HandyControlDemo_Shared/UserControl/Controls/SideMenuDemoCtl.xaml b/src/Shared/HandyControlDemo_Shared/UserControl/Controls/SideMenuDemoCtl.xaml index 141f9322..2b2ae21d 100644 --- a/src/Shared/HandyControlDemo_Shared/UserControl/Controls/SideMenuDemoCtl.xaml +++ b/src/Shared/HandyControlDemo_Shared/UserControl/Controls/SideMenuDemoCtl.xaml @@ -18,17 +18,17 @@ - + - + - + diff --git a/src/Shared/HandyControlDemo_Shared/ViewModel/Controls/SideMenuDemoViewModel.cs b/src/Shared/HandyControlDemo_Shared/ViewModel/Controls/SideMenuDemoViewModel.cs index e8b4e32a..9ae05f36 100644 --- a/src/Shared/HandyControlDemo_Shared/ViewModel/Controls/SideMenuDemoViewModel.cs +++ b/src/Shared/HandyControlDemo_Shared/ViewModel/Controls/SideMenuDemoViewModel.cs @@ -16,5 +16,10 @@ namespace HandyControlDemo.ViewModel new RelayCommand>(SwitchItem)).Value; private void SwitchItem(FunctionEventArgs info) => Growl.Info((info.Info as SideMenuItem)?.Header.ToString()); + + public RelayCommand SelectCmd => new Lazy>(() => + new RelayCommand(Select)).Value; + + private void Select(string header) => Growl.Success(header); } } \ No newline at end of file diff --git a/src/Shared/HandyControl_Shared/Controls/Input/SearchBar.cs b/src/Shared/HandyControl_Shared/Controls/Input/SearchBar.cs index 3b13e84e..75341640 100644 --- a/src/Shared/HandyControl_Shared/Controls/Input/SearchBar.cs +++ b/src/Shared/HandyControl_Shared/Controls/Input/SearchBar.cs @@ -52,14 +52,16 @@ namespace HandyControl.Controls Info = Text }); - if (Command == null) return; - if (Command is RoutedCommand command) + switch (Command) { - command.Execute(CommandParameter, CommandTarget); - } - else - { - Command.Execute(CommandParameter); + case null: + return; + case RoutedCommand command: + command.Execute(CommandParameter, CommandTarget); + break; + default: + Command.Execute(CommandParameter); + break; } } diff --git a/src/Shared/HandyControl_Shared/Controls/SideMenu/SideMenuItem.cs b/src/Shared/HandyControl_Shared/Controls/SideMenu/SideMenuItem.cs index 26f43578..1fee5b8c 100644 --- a/src/Shared/HandyControl_Shared/Controls/SideMenu/SideMenuItem.cs +++ b/src/Shared/HandyControl_Shared/Controls/SideMenu/SideMenuItem.cs @@ -1,4 +1,5 @@ -using System.Linq; +using System; +using System.Linq; using System.Windows; using System.Windows.Data; using System.Windows.Input; @@ -7,7 +8,7 @@ using HandyControl.Tools.Extension; namespace HandyControl.Controls { - public class SideMenuItem : HeaderedSimpleItemsControl, ISelectable + public class SideMenuItem : HeaderedSimpleItemsControl, ISelectable, ICommandSource { private bool _isMouseLeftButtonDown; @@ -68,7 +69,22 @@ namespace HandyControl.Controls } } - protected virtual void OnSelected(RoutedEventArgs e) => RaiseEvent(e); + protected virtual void OnSelected(RoutedEventArgs e) + { + RaiseEvent(e); + + switch (Command) + { + case null: + return; + case RoutedCommand command: + command.Execute(CommandParameter, CommandTarget); + break; + default: + Command.Execute(CommandParameter); + break; + } + } public static readonly RoutedEvent SelectedEvent = EventManager.RegisterRoutedEvent("Selected", RoutingStrategy.Bubble, @@ -148,5 +164,54 @@ namespace HandyControl.Controls ItemsHost.Show(isShow); } } + + public static readonly DependencyProperty CommandProperty = DependencyProperty.Register( + "Command", typeof(ICommand), typeof(SideMenuItem), new PropertyMetadata(default(ICommand), OnCommandChanged)); + + private static void OnCommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) + { + var ctl = (SideMenuItem)d; + if (e.OldValue is ICommand oldCommand) + { + oldCommand.CanExecuteChanged -= ctl.CanExecuteChanged; + } + if (e.NewValue is ICommand newCommand) + { + newCommand.CanExecuteChanged += ctl.CanExecuteChanged; + } + } + + public ICommand Command + { + get => (ICommand) GetValue(CommandProperty); + set => SetValue(CommandProperty, value); + } + + public static readonly DependencyProperty CommandParameterProperty = DependencyProperty.Register( + "CommandParameter", typeof(object), typeof(SideMenuItem), new PropertyMetadata(default(object))); + + public object CommandParameter + { + get => GetValue(CommandParameterProperty); + set => SetValue(CommandParameterProperty, value); + } + + public static readonly DependencyProperty CommandTargetProperty = DependencyProperty.Register( + "CommandTarget", typeof(IInputElement), typeof(SideMenuItem), new PropertyMetadata(default(IInputElement))); + + public IInputElement CommandTarget + { + get => (IInputElement) GetValue(CommandTargetProperty); + set => SetValue(CommandTargetProperty, value); + } + + private void CanExecuteChanged(object sender, EventArgs e) + { + if (Command == null) return; + + IsEnabled = Command is RoutedCommand command + ? command.CanExecute(CommandParameter, CommandTarget) + : Command.CanExecute(CommandParameter); + } } } \ No newline at end of file diff --git a/src/Shared/HandyControl_Shared/Controls/Window/BlurWindow.cs b/src/Shared/HandyControl_Shared/Controls/Window/BlurWindow.cs index 066ca9ce..8e461830 100644 --- a/src/Shared/HandyControl_Shared/Controls/Window/BlurWindow.cs +++ b/src/Shared/HandyControl_Shared/Controls/Window/BlurWindow.cs @@ -1,5 +1,4 @@ using System.Runtime.InteropServices; -using System.Windows.Interop; using System.Windows.Media; using HandyControl.Data; using HandyControl.Tools; @@ -18,18 +17,8 @@ namespace HandyControl.Controls internal static void EnableBlur(Window window) { - var accentPolicy = new ExternDllHelper.ACCENTPOLICY(); - var accentPolicySize = Marshal.SizeOf(accentPolicy); - - if (SystemVersionInfo >= SystemVersionInfo.Windows10_1809) - { - accentPolicy.AccentState = ExternDllHelper.ACCENTSTATE.ACCENT_ENABLE_ACRYLICBLURBEHIND; - } - else if (SystemVersionInfo >= SystemVersionInfo.Windows10) - { - accentPolicy.AccentState = ExternDllHelper.ACCENTSTATE.ACCENT_ENABLE_BLURBEHIND; - } - else + if (SystemVersionInfo < SystemVersionInfo.Windows10 || + SystemVersionInfo >= SystemVersionInfo.Windows10_1903) { var colorValue = ResourceHelper.GetResource(ResourceToken.BlurGradientValue); var color = ColorHelper.ToColor(colorValue); @@ -38,6 +27,13 @@ namespace HandyControl.Controls return; } + var accentPolicy = new ExternDllHelper.ACCENTPOLICY(); + var accentPolicySize = Marshal.SizeOf(accentPolicy); + + accentPolicy.AccentState = SystemVersionInfo < SystemVersionInfo.Windows10_1809 + ? ExternDllHelper.ACCENTSTATE.ACCENT_ENABLE_BLURBEHIND + : ExternDllHelper.ACCENTSTATE.ACCENT_ENABLE_ACRYLICBLURBEHIND; + accentPolicy.AccentFlags = 2; accentPolicy.GradientColor = ResourceHelper.GetResource(ResourceToken.BlurGradientValue); diff --git a/src/Shared/HandyControl_Shared/Data/SystemVersionInfo.cs b/src/Shared/HandyControl_Shared/Data/SystemVersionInfo.cs index 363e318a..6ea2a537 100644 --- a/src/Shared/HandyControl_Shared/Data/SystemVersionInfo.cs +++ b/src/Shared/HandyControl_Shared/Data/SystemVersionInfo.cs @@ -8,8 +8,11 @@ namespace HandyControl.Data public struct SystemVersionInfo { public static SystemVersionInfo Windows10 => new SystemVersionInfo(10, 0, 10240); + public static SystemVersionInfo Windows10_1809 => new SystemVersionInfo(10, 0, 17763); + public static SystemVersionInfo Windows10_1903 => new SystemVersionInfo(10, 0, 18362); + public SystemVersionInfo(int major, int minor, int build) { Major = major;