From fb7186246408fe60ed7ba383de5b418878e74020 Mon Sep 17 00:00:00 2001 From: NaBian <836904362@qq.com> Date: Wed, 31 Oct 2018 15:49:04 +0800 Subject: [PATCH] add slider style --- HandyControl/Controls/Attach/TipElement.cs | 23 ++ HandyControl/Controls/Slider/PreviewSlider.cs | 11 +- HandyControl/Controls/Slider/SliderCanvas.cs | 66 ++++ HandyControl/Data/Enum/TipPlacement.cs | 8 + HandyControl/HandyControl.csproj | 3 + HandyControl/Themes/Styles/Slider.xaml | 360 +++++++++++++++++- HandyControlDemo/Data/MessageToken.cs | 42 +- HandyControlDemo/Data/MessageToken.tt | 11 +- HandyControlDemo/HandyControlDemo.csproj | 8 + .../Properties/Langs/Lang.Designer.cs | 9 + .../Properties/Langs/Lang.en.resx | 3 + HandyControlDemo/Properties/Langs/Lang.resx | 3 + .../Img/LeftMainContent/Slider_16x.png | Bin 0 -> 177 bytes .../UserControl/Main/LeftMainContent.xaml | 5 + .../UserControl/Main/NoUserContent.xaml | 1 - .../UserControl/Styles/SliderDemoCtl.xaml | 24 ++ .../UserControl/Styles/SliderDemoCtl.xaml.cs | 11 + 17 files changed, 550 insertions(+), 38 deletions(-) create mode 100644 HandyControl/Controls/Attach/TipElement.cs create mode 100644 HandyControl/Controls/Slider/SliderCanvas.cs create mode 100644 HandyControl/Data/Enum/TipPlacement.cs create mode 100644 HandyControlDemo/Resources/Img/LeftMainContent/Slider_16x.png create mode 100644 HandyControlDemo/UserControl/Styles/SliderDemoCtl.xaml create mode 100644 HandyControlDemo/UserControl/Styles/SliderDemoCtl.xaml.cs diff --git a/HandyControl/Controls/Attach/TipElement.cs b/HandyControl/Controls/Attach/TipElement.cs new file mode 100644 index 00000000..7da236d6 --- /dev/null +++ b/HandyControl/Controls/Attach/TipElement.cs @@ -0,0 +1,23 @@ +using System.Windows; +using HandyControl.Data.Enum; + +// ReSharper disable once CheckNamespace +namespace HandyControl.Controls +{ + public class TipElement : DependencyObject + { + public static readonly DependencyProperty VisibilityProperty = DependencyProperty.RegisterAttached( + "Visibility", typeof(Visibility), typeof(TipElement), new PropertyMetadata(Visibility.Collapsed)); + + public static void SetVisibility(DependencyObject element, Visibility value) => element.SetValue(VisibilityProperty, value); + + public static Visibility GetVisibility(DependencyObject element) => (Visibility) element.GetValue(VisibilityProperty); + + public static readonly DependencyProperty PlacementProperty = DependencyProperty.RegisterAttached( + "Placement", typeof(TipPlacement), typeof(TipElement), new PropertyMetadata(default(TipPlacement))); + + public static void SetPlacement(DependencyObject element, TipPlacement value) => element.SetValue(PlacementProperty, value); + + public static TipPlacement GetPlacement(DependencyObject element) => (TipPlacement) element.GetValue(PlacementProperty); + } +} \ No newline at end of file diff --git a/HandyControl/Controls/Slider/PreviewSlider.cs b/HandyControl/Controls/Slider/PreviewSlider.cs index 9d4490d5..f4cdf158 100644 --- a/HandyControl/Controls/Slider/PreviewSlider.cs +++ b/HandyControl/Controls/Slider/PreviewSlider.cs @@ -1,6 +1,7 @@ using System; using System.Windows; using System.Windows.Controls; +using System.Windows.Controls.Primitives; using System.Windows.Input; using System.Windows.Media; using HandyControl.Data; @@ -9,14 +10,19 @@ using HandyControl.Data; namespace HandyControl.Controls { [TemplatePart(Name = PreviewContentKey, Type = typeof(ContentControl))] + [TemplatePart(Name = TracKKey, Type = typeof(Track))] public class PreviewSlider : Slider { private const string PreviewContentKey = "PART_ContentBorder"; + private const string TracKKey = "PART_Track"; + private ContentControl _previewContent; private TranslateTransform _transform; + private Track _track; + /// /// 预览内容 /// @@ -61,10 +67,10 @@ namespace HandyControl.Controls { base.OnMouseMove(e); - var p = e.GetPosition(this); + var p = e.GetPosition(_track); _transform.X = p.X - _previewContent.ActualWidth/2; - PreviewPosition = p.X / ActualWidth * Maximum; + PreviewPosition = p.X / _track.ActualWidth * Maximum; RaiseEvent(new FunctionEventArgs(PreviewPositionChangedEvent, this) { Info = PreviewPosition @@ -75,6 +81,7 @@ namespace HandyControl.Controls { base.OnApplyTemplate(); _previewContent = Template.FindName(PreviewContentKey, this) as ContentControl; + _track = Template.FindName(TracKKey, this) as Track; if (_previewContent != null) { diff --git a/HandyControl/Controls/Slider/SliderCanvas.cs b/HandyControl/Controls/Slider/SliderCanvas.cs new file mode 100644 index 00000000..ee4bade3 --- /dev/null +++ b/HandyControl/Controls/Slider/SliderCanvas.cs @@ -0,0 +1,66 @@ +using System; +using System.Windows; +using System.Windows.Controls; + +// ReSharper disable once CheckNamespace +namespace HandyControl.Controls +{ + internal class SliderCanvas : Canvas + { + internal static readonly DependencyProperty OrientationProperty = DependencyProperty.Register( + "Orientation", typeof(Orientation), typeof(SliderCanvas), new PropertyMetadata(default(Orientation))); + + internal Orientation Orientation + { + get => (Orientation) GetValue(OrientationProperty); + set => SetValue(OrientationProperty, value); + } + + protected override Size ArrangeOverride(Size arrangeSize) + { + foreach (UIElement internalChild in InternalChildren) + { + if (internalChild == null) continue; + + var x = 0.0; + var y = 0.0; + + if (Orientation == Orientation.Horizontal) + { + x = (arrangeSize.Width - internalChild.DesiredSize.Width) / 2; + + var top = GetTop(internalChild); + if (!double.IsNaN(top)) + { + y = top; + } + else + { + var bottom = GetBottom(internalChild); + if (!double.IsNaN(bottom)) + y = arrangeSize.Height - internalChild.DesiredSize.Height - bottom; + } + } + else + { + y = (arrangeSize.Height - internalChild.DesiredSize.Height) / 2; + + var left = GetLeft(internalChild); + if (!double.IsNaN(left)) + { + x = left; + } + else + { + var right = GetRight(internalChild); + if (!double.IsNaN(right)) + x = arrangeSize.Width - internalChild.DesiredSize.Width - right; + } + } + + internalChild.Arrange(new Rect(new Point(x, y), internalChild.DesiredSize)); + } + return arrangeSize; + } + } +} \ No newline at end of file diff --git a/HandyControl/Data/Enum/TipPlacement.cs b/HandyControl/Data/Enum/TipPlacement.cs new file mode 100644 index 00000000..53aeb715 --- /dev/null +++ b/HandyControl/Data/Enum/TipPlacement.cs @@ -0,0 +1,8 @@ +namespace HandyControl.Data.Enum +{ + public enum TipPlacement + { + TopLeft, + BottomRight, + } +} \ No newline at end of file diff --git a/HandyControl/HandyControl.csproj b/HandyControl/HandyControl.csproj index 6a376c28..3a4967d2 100644 --- a/HandyControl/HandyControl.csproj +++ b/HandyControl/HandyControl.csproj @@ -42,6 +42,7 @@ + @@ -60,8 +61,10 @@ + + True diff --git a/HandyControl/Themes/Styles/Slider.xaml b/HandyControl/Themes/Styles/Slider.xaml index ead6f251..0ee278ed 100644 --- a/HandyControl/Themes/Styles/Slider.xaml +++ b/HandyControl/Themes/Styles/Slider.xaml @@ -5,9 +5,11 @@ + - - + + + + + + + + + + + + + + + + + + + + + + + + @@ -388,9 +413,9 @@ - - - + + + @@ -398,7 +423,7 @@ - + @@ -416,4 +441,319 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +