refactor PreviewSlider
This commit is contained in:
1 parent
0546207f1a
commit
47a986270d
6 files changed
+535
-393
No files matched your search
@@ -2,22 +2,28 @@
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Controls.Primitives;
|
||||
using System.Windows.Data;
|
||||
using System.Windows.Documents;
|
||||
using System.Windows.Input;
|
||||
using System.Windows.Media;
|
||||
using HandyControl.Data;
|
||||
|
||||
using HandyControl.Interactivity;
|
||||
|
||||
namespace HandyControl.Controls
|
||||
{
|
||||
[TemplatePart(Name = PreviewContentKey, Type = typeof(ContentControl))]
|
||||
[TemplatePart(Name = TracKKey, Type = typeof(Track))]
|
||||
[TemplatePart(Name = TrackKey, Type = typeof(Track))]
|
||||
[TemplatePart(Name = ThumbKey, Type = typeof(FrameworkElement))]
|
||||
public class PreviewSlider : Slider
|
||||
{
|
||||
private const string PreviewContentKey = "PART_ContentBorder";
|
||||
private AdornerContainer _adorner;
|
||||
|
||||
private const string TracKKey = "PART_Track";
|
||||
private const string TrackKey = "PART_Track";
|
||||
|
||||
private ContentControl _previewContent;
|
||||
private const string ThumbKey = "PART_Thumb";
|
||||
|
||||
private FrameworkElement _previewContent;
|
||||
|
||||
private FrameworkElement _thumb;
|
||||
|
||||
private TranslateTransform _transform;
|
||||
|
||||
@@ -38,13 +44,28 @@ namespace HandyControl.Controls
|
||||
set => SetValue(PreviewContentProperty, value);
|
||||
}
|
||||
|
||||
public static readonly DependencyProperty PreviewPositionProperty = DependencyProperty.Register(
|
||||
"PreviewPosition", typeof(double), typeof(PreviewSlider), new PropertyMetadata(ValueBoxes.Double0Box));
|
||||
public static readonly DependencyProperty PreviewContentOffsetProperty = DependencyProperty.Register(
|
||||
"PreviewContentOffset", typeof(double), typeof(PreviewSlider), new PropertyMetadata(9.0));
|
||||
|
||||
public double PreviewContentOffset
|
||||
{
|
||||
get => (double) GetValue(PreviewContentOffsetProperty);
|
||||
set => SetValue(PreviewContentOffsetProperty, value);
|
||||
}
|
||||
|
||||
public static readonly DependencyProperty PreviewPositionProperty = DependencyProperty.RegisterAttached(
|
||||
"PreviewPosition", typeof(double), typeof(PreviewSlider), new FrameworkPropertyMetadata(ValueBoxes.Double0Box, FrameworkPropertyMetadataOptions.Inherits));
|
||||
|
||||
public static void SetPreviewPosition(DependencyObject element, double value)
|
||||
=> element.SetValue(PreviewPositionProperty, value);
|
||||
|
||||
public static double GetPreviewPosition(DependencyObject element)
|
||||
=> (double) element.GetValue(PreviewPositionProperty);
|
||||
|
||||
public double PreviewPosition
|
||||
{
|
||||
get => (double) GetValue(PreviewPositionProperty);
|
||||
set => SetValue(PreviewPositionProperty, value);
|
||||
get => GetPreviewPosition(_previewContent);
|
||||
set => SetPreviewPosition(_previewContent, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -67,26 +88,97 @@ namespace HandyControl.Controls
|
||||
{
|
||||
base.OnMouseMove(e);
|
||||
|
||||
var p = e.GetPosition(_track);
|
||||
_transform.X = p.X - _previewContent.ActualWidth/2;
|
||||
if (_previewContent == null) return;
|
||||
var p = e.GetPosition(_adorner);
|
||||
|
||||
if (Orientation == Orientation.Horizontal)
|
||||
{
|
||||
var pos = (e.GetPosition(this).X - _thumb.ActualWidth / 2) / _track.ActualWidth * Maximum;
|
||||
if (pos > Maximum || pos < 0)
|
||||
{
|
||||
if (_thumb.IsMouseCaptureWithin)
|
||||
{
|
||||
PreviewPosition = Value;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
_transform.X = p.X - _previewContent.ActualWidth / 2;
|
||||
_transform.Y = TranslatePoint(new Point(), _adorner).Y - _previewContent.ActualHeight - PreviewContentOffset;
|
||||
|
||||
PreviewPosition = _thumb.IsMouseCaptureWithin ? Value : pos;
|
||||
}
|
||||
else
|
||||
{
|
||||
var pos = Maximum - (e.GetPosition(this).Y - _thumb.ActualHeight / 2) / _track.ActualHeight * Maximum;
|
||||
if (pos > Maximum || pos < 0)
|
||||
{
|
||||
if (_thumb.IsMouseCaptureWithin)
|
||||
{
|
||||
PreviewPosition = Value;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
_transform.X = TranslatePoint(new Point(), _adorner).X - _previewContent.ActualWidth - PreviewContentOffset;
|
||||
_transform.Y = p.Y - _previewContent.ActualHeight / 2;
|
||||
|
||||
PreviewPosition = _thumb.IsMouseCaptureWithin ? Value : pos;
|
||||
}
|
||||
|
||||
PreviewPosition = p.X / _track.ActualWidth * Maximum;
|
||||
RaiseEvent(new FunctionEventArgs<double>(PreviewPositionChangedEvent, this)
|
||||
{
|
||||
Info = PreviewPosition
|
||||
});
|
||||
}
|
||||
|
||||
protected override void OnMouseEnter(MouseEventArgs e)
|
||||
{
|
||||
base.OnMouseEnter(e);
|
||||
|
||||
if (_adorner == null)
|
||||
{
|
||||
var layer = AdornerLayer.GetAdornerLayer(this);
|
||||
if (layer == null) return;
|
||||
_adorner = new AdornerContainer(layer)
|
||||
{
|
||||
Child = _previewContent
|
||||
};
|
||||
layer.Add(_adorner);
|
||||
}
|
||||
}
|
||||
|
||||
protected override void OnMouseLeave(MouseEventArgs e)
|
||||
{
|
||||
base.OnMouseLeave(e);
|
||||
|
||||
var layer = AdornerLayer.GetAdornerLayer(this);
|
||||
if (layer == null) return;
|
||||
layer.Remove(_adorner);
|
||||
_adorner.Child = null;
|
||||
_adorner = null;
|
||||
}
|
||||
|
||||
public override void OnApplyTemplate()
|
||||
{
|
||||
base.OnApplyTemplate();
|
||||
_previewContent = Template.FindName(PreviewContentKey, this) as ContentControl;
|
||||
_track = Template.FindName(TracKKey, this) as Track;
|
||||
|
||||
var contentControl = new ContentControl
|
||||
{
|
||||
DataContext = this
|
||||
};
|
||||
contentControl.SetBinding(ContentControl.ContentProperty, new Binding(PreviewContentProperty.Name) { Source = this });
|
||||
_previewContent = contentControl;
|
||||
|
||||
_track = Template.FindName(TrackKey, this) as Track;
|
||||
_thumb = Template.FindName(ThumbKey, this) as FrameworkElement;
|
||||
|
||||
if (_previewContent != null)
|
||||
{
|
||||
_transform = new TranslateTransform();
|
||||
_previewContent.RenderTransform = _transform;
|
||||
_previewContent.HorizontalAlignment = HorizontalAlignment.Left;
|
||||
_previewContent.VerticalAlignment = VerticalAlignment.Top;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user