From af4f8d8fc3a1d5d8787a8a28fea24c6bd3be9ee6 Mon Sep 17 00:00:00 2001 From: Yimeng Wu Date: Tue, 22 Oct 2019 17:21:06 +0800 Subject: [PATCH] Move focus visual properties to FocusVisualHelper --- .../HyperlinkButton/HyperlinkButton.xaml | 2 +- .../RadioButtons/RadioButtons.xaml | 2 +- .../ToggleSwitch/ToggleSwitch.xaml | 4 +- ModernWpf.SampleApp/App.xaml | 2 +- .../Controls/Primitives/FocusVisualHelper.cs | 141 ++++++++++++++++-- .../Primitives/FrameworkElementHelper.cs | 129 ---------------- ModernWpf/Styles/Button.xaml | 2 +- ModernWpf/Styles/Calendar.xaml | 6 +- ModernWpf/Styles/CheckBox.xaml | 2 +- ModernWpf/Styles/DataGrid.xaml | 2 +- ModernWpf/Styles/Expander.xaml | 2 +- ModernWpf/Styles/RadioButton.xaml | 2 +- ModernWpf/Styles/RepeatButton.xaml | 3 +- ModernWpf/Styles/Slider.xaml | 11 +- ModernWpf/Styles/TabControl.xaml | 3 +- ModernWpf/Styles/ToggleButton.xaml | 3 +- 16 files changed, 152 insertions(+), 164 deletions(-) delete mode 100644 ModernWpf/Controls/Primitives/FrameworkElementHelper.cs diff --git a/ModernWpf.Controls/HyperlinkButton/HyperlinkButton.xaml b/ModernWpf.Controls/HyperlinkButton/HyperlinkButton.xaml index 69bb0ebb..0843a58a 100644 --- a/ModernWpf.Controls/HyperlinkButton/HyperlinkButton.xaml +++ b/ModernWpf.Controls/HyperlinkButton/HyperlinkButton.xaml @@ -17,7 +17,7 @@ - + diff --git a/ModernWpf.Controls/RadioButtons/RadioButtons.xaml b/ModernWpf.Controls/RadioButtons/RadioButtons.xaml index 038cbaa5..c7366f3a 100644 --- a/ModernWpf.Controls/RadioButtons/RadioButtons.xaml +++ b/ModernWpf.Controls/RadioButtons/RadioButtons.xaml @@ -23,7 +23,7 @@ - + diff --git a/ModernWpf.Controls/ToggleSwitch/ToggleSwitch.xaml b/ModernWpf.Controls/ToggleSwitch/ToggleSwitch.xaml index 7d677c4e..dddb98b7 100644 --- a/ModernWpf.Controls/ToggleSwitch/ToggleSwitch.xaml +++ b/ModernWpf.Controls/ToggleSwitch/ToggleSwitch.xaml @@ -26,7 +26,7 @@ - + @@ -224,7 +224,7 @@ + Margin="{TemplateBinding ui:FocusVisualHelper.FocusVisualMargin}" /> - + diff --git a/ModernWpf/Controls/Primitives/FocusVisualHelper.cs b/ModernWpf/Controls/Primitives/FocusVisualHelper.cs index b9560211..4fee47d9 100644 --- a/ModernWpf/Controls/Primitives/FocusVisualHelper.cs +++ b/ModernWpf/Controls/Primitives/FocusVisualHelper.cs @@ -7,6 +7,127 @@ namespace ModernWpf.Controls.Primitives { public static class FocusVisualHelper { + #region FocusVisualPrimaryBrush + + public static Brush GetFocusVisualPrimaryBrush(FrameworkElement element) + { + return (Brush)element.GetValue(FocusVisualPrimaryBrushProperty); + } + + public static void SetFocusVisualPrimaryBrush(FrameworkElement element, Brush value) + { + element.SetValue(FocusVisualPrimaryBrushProperty, value); + } + + public static readonly DependencyProperty FocusVisualPrimaryBrushProperty = + DependencyProperty.RegisterAttached( + "FocusVisualPrimaryBrush", + typeof(Brush), + typeof(FocusVisualHelper), + null); + + #endregion + + #region FocusVisualSecondaryBrush + + public static Brush GetFocusVisualSecondaryBrush(FrameworkElement element) + { + return (Brush)element.GetValue(FocusVisualSecondaryBrushProperty); + } + + public static void SetFocusVisualSecondaryBrush(FrameworkElement element, Brush value) + { + element.SetValue(FocusVisualSecondaryBrushProperty, value); + } + + public static readonly DependencyProperty FocusVisualSecondaryBrushProperty = + DependencyProperty.RegisterAttached( + "FocusVisualSecondaryBrush", + typeof(Brush), + typeof(FocusVisualHelper), + null); + + #endregion + + #region FocusVisualPrimaryThickness + + public static Thickness GetFocusVisualPrimaryThickness(FrameworkElement element) + { + return (Thickness)element.GetValue(FocusVisualPrimaryThicknessProperty); + } + + public static void SetFocusVisualPrimaryThickness(FrameworkElement element, Thickness value) + { + element.SetValue(FocusVisualPrimaryThicknessProperty, value); + } + + public static readonly DependencyProperty FocusVisualPrimaryThicknessProperty = + DependencyProperty.RegisterAttached( + "FocusVisualPrimaryThickness", + typeof(Thickness), + typeof(FocusVisualHelper), + new FrameworkPropertyMetadata(new Thickness(1))); + + #endregion + + #region FocusVisualSecondaryThickness + + public static Thickness GetFocusVisualSecondaryThickness(FrameworkElement element) + { + return (Thickness)element.GetValue(FocusVisualSecondaryThicknessProperty); + } + + public static void SetFocusVisualSecondaryThickness(FrameworkElement element, Thickness value) + { + element.SetValue(FocusVisualSecondaryThicknessProperty, value); + } + + public static readonly DependencyProperty FocusVisualSecondaryThicknessProperty = + DependencyProperty.RegisterAttached( + "FocusVisualSecondaryThickness", + typeof(Thickness), + typeof(FocusVisualHelper), + new FrameworkPropertyMetadata(new Thickness(1))); + + #endregion + + #region FocusVisualMargin + + /// + /// Gets the outer margin of the focus visual for a FrameworkElement. + /// + /// The element from which to read the property value. + /// + /// Provides margin values for the focus visual. The default value is a default Thickness + /// with all properties (dimensions) equal to 0. + /// + public static Thickness GetFocusVisualMargin(FrameworkElement element) + { + return (Thickness)element.GetValue(FocusVisualMarginProperty); + } + + /// + /// Sets the outer margin of the focus visual for a FrameworkElement. + /// + /// The element on which to set the attached property. + /// The property value to set. + public static void SetFocusVisualMargin(FrameworkElement element, Thickness value) + { + element.SetValue(FocusVisualMarginProperty, value); + } + + /// + /// Identifies the FocusVisualMargin dependency property. + /// + public static readonly DependencyProperty FocusVisualMarginProperty = + DependencyProperty.RegisterAttached( + "FocusVisualMargin", + typeof(Thickness), + typeof(FocusVisualHelper), + null); + + #endregion + #region IsSystemFocusVisual public static bool GetIsSystemFocusVisual(Control control) @@ -31,11 +152,11 @@ private static void OnIsSystemFocusVisualChanged(DependencyObject d, DependencyP var control = (Control)d; if ((bool)e.NewValue) { - control.IsVisibleChanged += OnControlIsVisibleChanged; + control.IsVisibleChanged += OnFocusVisualIsVisibleChanged; } else { - control.IsVisibleChanged -= OnControlIsVisibleChanged; + control.IsVisibleChanged -= OnFocusVisualIsVisibleChanged; control.ClearValue(FrameworkElement.MarginProperty); } } @@ -86,26 +207,26 @@ private static void SetFocusedElement(Control control, FrameworkElement value) #endregion - private static void OnControlIsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e) + private static void OnFocusVisualIsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e) { - var control = (Control)sender; + var focusVisual = (Control)sender; if ((bool)e.NewValue) { - if ((VisualTreeHelper.GetParent(control) as Adorner)?.AdornedElement is FrameworkElement focusedElement) + if ((VisualTreeHelper.GetParent(focusVisual) as Adorner)?.AdornedElement is FrameworkElement focusedElement) { SetIsSystemFocusVisualVisible(focusedElement, true); - control.Margin = FrameworkElementHelper.GetFocusVisualMargin(focusedElement); - SetFocusedElement(control, focusedElement); + focusVisual.Margin = FocusVisualHelper.GetFocusVisualMargin(focusedElement); + SetFocusedElement(focusVisual, focusedElement); } } else { - FrameworkElement focusedElement = GetFocusedElement(control); + FrameworkElement focusedElement = GetFocusedElement(focusVisual); if (focusedElement != null) { focusedElement.ClearValue(IsSystemFocusVisualVisiblePropertyKey); - control.ClearValue(FrameworkElement.MarginProperty); - control.ClearValue(FocusedElementProperty); + focusVisual.ClearValue(FrameworkElement.MarginProperty); + focusVisual.ClearValue(FocusedElementProperty); } } } diff --git a/ModernWpf/Controls/Primitives/FrameworkElementHelper.cs b/ModernWpf/Controls/Primitives/FrameworkElementHelper.cs deleted file mode 100644 index 07f991bb..00000000 --- a/ModernWpf/Controls/Primitives/FrameworkElementHelper.cs +++ /dev/null @@ -1,129 +0,0 @@ -using System.Windows; -using System.Windows.Media; - -namespace ModernWpf.Controls.Primitives -{ - public static class FrameworkElementHelper - { - #region FocusVisualPrimaryBrush - - public static Brush GetFocusVisualPrimaryBrush(FrameworkElement element) - { - return (Brush)element.GetValue(FocusVisualPrimaryBrushProperty); - } - - public static void SetFocusVisualPrimaryBrush(FrameworkElement element, Brush value) - { - element.SetValue(FocusVisualPrimaryBrushProperty, value); - } - - public static readonly DependencyProperty FocusVisualPrimaryBrushProperty = - DependencyProperty.RegisterAttached( - "FocusVisualPrimaryBrush", - typeof(Brush), - typeof(FrameworkElementHelper), - null); - - #endregion - - #region FocusVisualSecondaryBrush - - public static Brush GetFocusVisualSecondaryBrush(FrameworkElement element) - { - return (Brush)element.GetValue(FocusVisualSecondaryBrushProperty); - } - - public static void SetFocusVisualSecondaryBrush(FrameworkElement element, Brush value) - { - element.SetValue(FocusVisualSecondaryBrushProperty, value); - } - - public static readonly DependencyProperty FocusVisualSecondaryBrushProperty = - DependencyProperty.RegisterAttached( - "FocusVisualSecondaryBrush", - typeof(Brush), - typeof(FrameworkElementHelper), - null); - - #endregion - - #region FocusVisualPrimaryThickness - - public static Thickness GetFocusVisualPrimaryThickness(FrameworkElement element) - { - return (Thickness)element.GetValue(FocusVisualPrimaryThicknessProperty); - } - - public static void SetFocusVisualPrimaryThickness(FrameworkElement element, Thickness value) - { - element.SetValue(FocusVisualPrimaryThicknessProperty, value); - } - - public static readonly DependencyProperty FocusVisualPrimaryThicknessProperty = - DependencyProperty.RegisterAttached( - "FocusVisualPrimaryThickness", - typeof(Thickness), - typeof(FrameworkElementHelper), - new FrameworkPropertyMetadata(new Thickness(1))); - - #endregion - - #region FocusVisualSecondaryThickness - - public static Thickness GetFocusVisualSecondaryThickness(FrameworkElement element) - { - return (Thickness)element.GetValue(FocusVisualSecondaryThicknessProperty); - } - - public static void SetFocusVisualSecondaryThickness(FrameworkElement element, Thickness value) - { - element.SetValue(FocusVisualSecondaryThicknessProperty, value); - } - - public static readonly DependencyProperty FocusVisualSecondaryThicknessProperty = - DependencyProperty.RegisterAttached( - "FocusVisualSecondaryThickness", - typeof(Thickness), - typeof(FrameworkElementHelper), - new FrameworkPropertyMetadata(new Thickness(1))); - - #endregion - - #region FocusVisualMargin - - /// - /// Gets the outer margin of the focus visual for a FrameworkElement. - /// - /// The element from which to read the property value. - /// - /// Provides margin values for the focus visual. The default value is a default Thickness - /// with all properties (dimensions) equal to 0. - /// - public static Thickness GetFocusVisualMargin(FrameworkElement element) - { - return (Thickness)element.GetValue(FocusVisualMarginProperty); - } - - /// - /// Sets the outer margin of the focus visual for a FrameworkElement. - /// - /// The element on which to set the attached property. - /// The property value to set. - public static void SetFocusVisualMargin(FrameworkElement element, Thickness value) - { - element.SetValue(FocusVisualMarginProperty, value); - } - - /// - /// Identifies the FocusVisualMargin dependency property. - /// - public static readonly DependencyProperty FocusVisualMarginProperty = - DependencyProperty.RegisterAttached( - "FocusVisualMargin", - typeof(Thickness), - typeof(FrameworkElementHelper), - null); - - #endregion - } -} diff --git a/ModernWpf/Styles/Button.xaml b/ModernWpf/Styles/Button.xaml index 265b8b7f..be960f18 100644 --- a/ModernWpf/Styles/Button.xaml +++ b/ModernWpf/Styles/Button.xaml @@ -22,7 +22,7 @@ - + diff --git a/ModernWpf/Styles/Calendar.xaml b/ModernWpf/Styles/Calendar.xaml index 16114c2e..df592c48 100644 --- a/ModernWpf/Styles/Calendar.xaml +++ b/ModernWpf/Styles/Calendar.xaml @@ -41,7 +41,7 @@ - + @@ -203,7 +203,7 @@ - + @@ -311,7 +311,7 @@ - + diff --git a/ModernWpf/Styles/CheckBox.xaml b/ModernWpf/Styles/CheckBox.xaml index a8486c43..dfe3456f 100644 --- a/ModernWpf/Styles/CheckBox.xaml +++ b/ModernWpf/Styles/CheckBox.xaml @@ -24,7 +24,7 @@ - + diff --git a/ModernWpf/Styles/DataGrid.xaml b/ModernWpf/Styles/DataGrid.xaml index 4a0f1f5b..81a534aa 100644 --- a/ModernWpf/Styles/DataGrid.xaml +++ b/ModernWpf/Styles/DataGrid.xaml @@ -898,7 +898,7 @@ Foreground="{TemplateBinding Foreground}" IsChecked="{Binding IsExpanded, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}" Style="{StaticResource DataGridRowGroupHeaderStyle}" - primitives:FrameworkElementHelper.FocusVisualMargin="0" /> + primitives:FocusVisualHelper.FocusVisualMargin="0" /> + primitives:FocusVisualHelper.FocusVisualMargin="-3" /> - + diff --git a/ModernWpf/Styles/RepeatButton.xaml b/ModernWpf/Styles/RepeatButton.xaml index 8de81281..3ce5427f 100644 --- a/ModernWpf/Styles/RepeatButton.xaml +++ b/ModernWpf/Styles/RepeatButton.xaml @@ -2,7 +2,6 @@