Merge branch 'master' into pr/84
This commit is contained in:
commit
6d4cbbb7d2
31 files changed
+726
-187
No files matched your search
@@ -18,11 +18,11 @@ namespace HandyControl.Controls
|
||||
/// <summary>
|
||||
/// 是否隐藏元素
|
||||
/// </summary>
|
||||
public static readonly DependencyProperty HiddenElementProperty = DependencyProperty.RegisterAttached(
|
||||
"HiddenElement", typeof(bool), typeof(StatusSwitchElement), new PropertyMetadata(ValueBoxes.FalseBox));
|
||||
public static readonly DependencyProperty HideUncheckedElementProperty = DependencyProperty.RegisterAttached(
|
||||
"HideUncheckedElement", typeof(bool), typeof(StatusSwitchElement), new PropertyMetadata(ValueBoxes.FalseBox));
|
||||
|
||||
public static void SetHiddenElement(DependencyObject element, bool value) => element.SetValue(HiddenElementProperty, value);
|
||||
public static void SetHideUncheckedElement(DependencyObject element, bool value) => element.SetValue(HideUncheckedElementProperty, value);
|
||||
|
||||
public static bool GetHiddenElement(DependencyObject element) => (bool) element.GetValue(HiddenElementProperty);
|
||||
public static bool GetHideUncheckedElement(DependencyObject element) => (bool) element.GetValue(HideUncheckedElementProperty);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
using System;
|
||||
using System.Windows;
|
||||
using System.Windows.Input;
|
||||
using System.Windows.Media;
|
||||
using HandyControl.Data;
|
||||
using HandyControl.Tools;
|
||||
using HandyControl.Tools.Interop;
|
||||
|
||||
namespace HandyControl.Controls
|
||||
{
|
||||
internal class ColorDropper
|
||||
{
|
||||
private bool _cursorIsSetted;
|
||||
|
||||
private Cursor _dropperCursor;
|
||||
|
||||
private readonly ColorPicker _colorPicker;
|
||||
|
||||
public ColorDropper(ColorPicker colorPicker)
|
||||
{
|
||||
_colorPicker = colorPicker;
|
||||
}
|
||||
|
||||
public void Update(bool isShow)
|
||||
{
|
||||
if (isShow)
|
||||
{
|
||||
if (_dropperCursor == null)
|
||||
{
|
||||
var info = Application.GetResourceStream(new Uri("pack://application:,,,/HandyControl;Component/Resources/dropper.cur"));
|
||||
if (info != null)
|
||||
{
|
||||
_dropperCursor = new Cursor(info.Stream);
|
||||
}
|
||||
}
|
||||
|
||||
if (_dropperCursor == null) return;
|
||||
|
||||
MouseHook.Start();
|
||||
MouseHook.StatusChanged += MouseHook_StatusChanged;
|
||||
}
|
||||
else
|
||||
{
|
||||
Mouse.OverrideCursor = Cursors.Arrow;
|
||||
MouseHook.Stop();
|
||||
MouseHook.StatusChanged -= MouseHook_StatusChanged;
|
||||
}
|
||||
}
|
||||
|
||||
private void MouseHook_StatusChanged(object sender, MouseHookEventArgs e)
|
||||
{
|
||||
var window = System.Windows.Window.GetWindow(_colorPicker);
|
||||
if (window == null)
|
||||
{
|
||||
UpdateCursor(false);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (!_colorPicker.IsMouseOver && window.IsMouseOver)
|
||||
{
|
||||
UpdateCursor(true);
|
||||
if (e.Message == MouseHookMessageType.LeftButtonDown)
|
||||
{
|
||||
var brush = new SolidColorBrush(GetColorAt(e.Point.X, e.Point.Y));
|
||||
_colorPicker.SelectedBrush = brush;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
UpdateCursor(false);
|
||||
}
|
||||
}
|
||||
|
||||
private void UpdateCursor(bool isDropper)
|
||||
{
|
||||
if (isDropper)
|
||||
{
|
||||
Mouse.Captured?.ReleaseMouseCapture();
|
||||
|
||||
if (!_cursorIsSetted)
|
||||
{
|
||||
Mouse.OverrideCursor = _dropperCursor;
|
||||
_cursorIsSetted = true;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (_cursorIsSetted)
|
||||
{
|
||||
Mouse.OverrideCursor = Cursors.Arrow;
|
||||
_cursorIsSetted = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static Color GetColorAt(int x, int y)
|
||||
{
|
||||
var desk = UnsafeNativeMethods.GetDesktopWindow();
|
||||
var dc = UnsafeNativeMethods.GetWindowDC(desk);
|
||||
var a = (int)UnsafeNativeMethods.GetPixel(dc, x, y);
|
||||
UnsafeNativeMethods.ReleaseDC(desk, dc);
|
||||
return Color.FromArgb(255, (byte) ((a >> 0) & 0xff), (byte) ((a >> 8) & 0xff), (byte) ((a >> 16) & 0xff));
|
||||
}
|
||||
}
|
||||
}
|
||||
+55
-2
@@ -3,10 +3,12 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Controls.Primitives;
|
||||
using System.Windows.Input;
|
||||
using System.Windows.Media;
|
||||
using HandyControl.Data;
|
||||
using HandyControl.Interactivity;
|
||||
using HandyControl.Tools;
|
||||
using HandyControl.Tools.Extension;
|
||||
|
||||
namespace HandyControl.Controls
|
||||
@@ -21,6 +23,7 @@ namespace HandyControl.Controls
|
||||
[TemplatePart(Name = ElementSliderColor, Type = typeof(Panel))]
|
||||
[TemplatePart(Name = ElementSliderOpacity, Type = typeof(Panel))]
|
||||
[TemplatePart(Name = ElementPanelRgb, Type = typeof(Panel))]
|
||||
[TemplatePart(Name = ElementButtonDropper, Type = typeof(ToggleButton))]
|
||||
public class ColorPicker : Control, ISingleOpen
|
||||
{
|
||||
#region Constants
|
||||
@@ -32,11 +35,16 @@ namespace HandyControl.Controls
|
||||
private const string ElementSliderColor = "PART_SliderColor";
|
||||
private const string ElementSliderOpacity = "PART_SliderOpacity";
|
||||
private const string ElementPanelRgb = "PART_PanelRgb";
|
||||
private const string ElementButtonDropper = "PART_ButtonDropper";
|
||||
|
||||
#endregion Constants
|
||||
|
||||
#region Data
|
||||
|
||||
private ColorDropper _colorDropper;
|
||||
|
||||
private ToggleButton _toggleButtonDropper;
|
||||
|
||||
private Panel _panelRgb;
|
||||
|
||||
private Border _borderColor;
|
||||
@@ -53,6 +61,8 @@ namespace HandyControl.Controls
|
||||
|
||||
private bool _appliedTemplate;
|
||||
|
||||
private bool _disposed;
|
||||
|
||||
/// <summary>
|
||||
/// 当前显示的颜色类型
|
||||
/// </summary>
|
||||
@@ -332,6 +342,11 @@ namespace HandyControl.Controls
|
||||
}
|
||||
}
|
||||
|
||||
~ColorPicker()
|
||||
{
|
||||
Dispose(false);
|
||||
}
|
||||
|
||||
public ColorPicker()
|
||||
{
|
||||
CommandBindings.Add(new CommandBinding(ControlCommands.Confirm, ButtonConfirm_OnClick));
|
||||
@@ -361,6 +376,11 @@ namespace HandyControl.Controls
|
||||
_sliderOpacity.ValueChanged -= SliderOpacity_OnValueChanged;
|
||||
}
|
||||
|
||||
if (_toggleButtonDropper != null)
|
||||
{
|
||||
_toggleButtonDropper.Click -= ToggleButtonDropper_Click;
|
||||
}
|
||||
|
||||
_panelColor?.Children.Clear();
|
||||
|
||||
_panelRgb?.RemoveHandler(NumericUpDown.ValueChangedEvent, new EventHandler<FunctionEventArgs<double>>(NumericUpDownRgb_OnValueChanged));
|
||||
@@ -374,6 +394,7 @@ namespace HandyControl.Controls
|
||||
_sliderColor = GetTemplateChild(ElementSliderColor) as Slider;
|
||||
_sliderOpacity = GetTemplateChild(ElementSliderOpacity) as Slider;
|
||||
_panelRgb = GetTemplateChild(ElementPanelRgb) as Panel;
|
||||
_toggleButtonDropper = GetTemplateChild(ElementButtonDropper) as ToggleButton;
|
||||
|
||||
if (_sliderColor != null)
|
||||
{
|
||||
@@ -395,6 +416,11 @@ namespace HandyControl.Controls
|
||||
collection.Add(behavior);
|
||||
}
|
||||
|
||||
if (_toggleButtonDropper != null)
|
||||
{
|
||||
_toggleButtonDropper.Click += ToggleButtonDropper_Click;
|
||||
}
|
||||
|
||||
_appliedTemplate = true;
|
||||
if (_isLoaded)
|
||||
{
|
||||
@@ -428,7 +454,7 @@ namespace HandyControl.Controls
|
||||
var button = new Button
|
||||
{
|
||||
Margin = new Thickness(0, 0, 12, 12),
|
||||
Style = FindResource("ButtonCustom") as Style,
|
||||
Style = ResourceHelper.GetResource<Style>(ResourceToken.ButtonCustom),
|
||||
Content = new Border
|
||||
{
|
||||
Background = brush,
|
||||
@@ -675,8 +701,35 @@ namespace HandyControl.Controls
|
||||
|
||||
private void ButtonCancel_OnClick(object sender, RoutedEventArgs e) => RaiseEvent(new RoutedEventArgs(CanceledEvent));
|
||||
|
||||
public void Dispose() => System.Windows.Window.GetWindow(this)?.Close();
|
||||
private void ToggleButtonDropper_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
if (_colorDropper == null)
|
||||
{
|
||||
_colorDropper = new ColorDropper(this);
|
||||
}
|
||||
// ReSharper disable once PossibleInvalidOperationException
|
||||
_colorDropper.Update(_toggleButtonDropper.IsChecked.Value);
|
||||
}
|
||||
|
||||
protected virtual void Dispose(bool disposing)
|
||||
{
|
||||
if (_disposed) return;
|
||||
|
||||
Dispatcher.BeginInvoke(new Action(() =>
|
||||
{
|
||||
_colorDropper?.Update(false);
|
||||
System.Windows.Window.GetWindow(this)?.Close();
|
||||
}));
|
||||
_disposed = true;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Dispose(true);
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
public bool CanDispose { get; } = true;
|
||||
|
||||
}
|
||||
}
|
||||
@@ -99,15 +99,6 @@ namespace HandyControl.Controls
|
||||
set => SetValue(DateTimeFormatProperty, value);
|
||||
}
|
||||
|
||||
public static readonly DependencyProperty ClockStyleProperty = DependencyProperty.Register(
|
||||
"ClockStyle", typeof(Style), typeof(DateTimePicker), new PropertyMetadata(default(Style)));
|
||||
|
||||
public Style ClockStyle
|
||||
{
|
||||
get => (Style) GetValue(ClockStyleProperty);
|
||||
set => SetValue(ClockStyleProperty, value);
|
||||
}
|
||||
|
||||
public static readonly DependencyProperty CalendarStyleProperty = DependencyProperty.Register(
|
||||
"CalendarStyle", typeof(Style), typeof(DateTimePicker), new PropertyMetadata(default(Style)));
|
||||
|
||||
|
||||
@@ -34,8 +34,6 @@ namespace HandyControl.Controls
|
||||
|
||||
#region Data
|
||||
|
||||
private Clock _clock;
|
||||
|
||||
private string _defaultText;
|
||||
|
||||
private ButtonBase _dropDownButton;
|
||||
@@ -79,7 +77,7 @@ namespace HandyControl.Controls
|
||||
|
||||
public TimePicker()
|
||||
{
|
||||
InitClock();
|
||||
Clock = new Clock();
|
||||
CommandBindings.Add(new CommandBinding(ControlCommands.Clear, (s, e) =>
|
||||
{
|
||||
ClearValue(SelectedTimeProperty);
|
||||
@@ -103,18 +101,6 @@ namespace HandyControl.Controls
|
||||
|
||||
#endregion TimeFormat
|
||||
|
||||
#region ClockStyle
|
||||
|
||||
public Style ClockStyle
|
||||
{
|
||||
get => (Style)GetValue(ClockStyleProperty);
|
||||
set => SetValue(ClockStyleProperty, value);
|
||||
}
|
||||
|
||||
public static readonly DependencyProperty ClockStyleProperty = DependencyProperty.Register("ClockStyle", typeof(Style), typeof(TimePicker));
|
||||
|
||||
#endregion CalendarStyle
|
||||
|
||||
#region DisplayTime
|
||||
|
||||
public DateTime DisplayTime
|
||||
@@ -133,8 +119,8 @@ namespace HandyControl.Controls
|
||||
private static object CoerceDisplayTime(DependencyObject d, object value)
|
||||
{
|
||||
var dp = (TimePicker)d;
|
||||
dp._clock.DisplayTime = (DateTime)value;
|
||||
return dp._clock.DisplayTime;
|
||||
dp.Clock.DisplayTime = (DateTime)value;
|
||||
return dp.Clock.DisplayTime;
|
||||
}
|
||||
|
||||
#endregion DisplayTime
|
||||
@@ -170,7 +156,7 @@ namespace HandyControl.Controls
|
||||
|
||||
dp.Dispatcher.BeginInvoke(DispatcherPriority.Input, (Action)delegate
|
||||
{
|
||||
dp._clock.Focus();
|
||||
dp.Clock.Focus();
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -212,8 +198,8 @@ namespace HandyControl.Controls
|
||||
private static object CoerceSelectedTime(DependencyObject d, object value)
|
||||
{
|
||||
var dp = (TimePicker)d;
|
||||
dp._clock.SelectedTime = (DateTime?)value;
|
||||
return dp._clock.SelectedTime;
|
||||
dp.Clock.SelectedTime = (DateTime?)value;
|
||||
return dp.Clock.SelectedTime;
|
||||
}
|
||||
|
||||
#endregion SelectedDate
|
||||
@@ -306,6 +292,35 @@ namespace HandyControl.Controls
|
||||
set => SetValue(ShowClearButtonProperty, value);
|
||||
}
|
||||
|
||||
public static readonly DependencyProperty ClockProperty = DependencyProperty.Register(
|
||||
"Clock", typeof(ClockBase), typeof(TimePicker), new FrameworkPropertyMetadata(default(Clock), FrameworkPropertyMetadataOptions.NotDataBindable, OnClockChanged));
|
||||
|
||||
private static void OnClockChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
|
||||
{
|
||||
var ctl = (TimePicker) d;
|
||||
var oldClock = e.OldValue as ClockBase;
|
||||
var newClock = e.NewValue as ClockBase;
|
||||
|
||||
if (oldClock != null)
|
||||
{
|
||||
oldClock.SelectedTimeChanged -= ctl.Clock_SelectedTimeChanged;
|
||||
oldClock.Confirmed -= ctl.Clock_Confirmed;
|
||||
}
|
||||
|
||||
if (newClock != null)
|
||||
{
|
||||
newClock.ShowConfirmButton = true;
|
||||
newClock.SelectedTimeChanged += ctl.Clock_SelectedTimeChanged;
|
||||
newClock.Confirmed += ctl.Clock_Confirmed;
|
||||
}
|
||||
}
|
||||
|
||||
public ClockBase Clock
|
||||
{
|
||||
get => (ClockBase) GetValue(ClockProperty);
|
||||
set => SetValue(ClockProperty, value);
|
||||
}
|
||||
|
||||
#endregion Public Properties
|
||||
|
||||
#region Public Methods
|
||||
@@ -374,7 +389,7 @@ namespace HandyControl.Controls
|
||||
_popUp.PreviewMouseLeftButtonDown += PopUp_PreviewMouseLeftButtonDown;
|
||||
_popUp.Opened += PopUp_Opened;
|
||||
_popUp.Closed += PopUp_Closed;
|
||||
_popUp.Child = _clock;
|
||||
_popUp.Child = Clock;
|
||||
|
||||
if (IsDropDownOpen)
|
||||
{
|
||||
@@ -439,20 +454,7 @@ namespace HandyControl.Controls
|
||||
throw new Exception();
|
||||
}
|
||||
|
||||
private void InitClock()
|
||||
{
|
||||
_clock = new Clock
|
||||
{
|
||||
ShowConfirmButton = true
|
||||
};
|
||||
_clock.SelectedTimeChanged += Clock_SelectedTimeChanged;
|
||||
_clock.Confirmed += Clock_Confirmed;
|
||||
}
|
||||
|
||||
private void TextBox_LostFocus(object sender, RoutedEventArgs e)
|
||||
{
|
||||
SetSelectedTime();
|
||||
}
|
||||
private void TextBox_LostFocus(object sender, RoutedEventArgs e) => SetSelectedTime();
|
||||
|
||||
private void SetIsHandlerSuspended(DependencyProperty property, bool value)
|
||||
{
|
||||
@@ -560,7 +562,7 @@ namespace HandyControl.Controls
|
||||
SetCurrentValue(IsDropDownOpenProperty, ValueBoxes.TrueBox);
|
||||
}
|
||||
|
||||
_clock?.MoveFocus(new TraversalRequest(FocusNavigationDirection.First));
|
||||
Clock?.MoveFocus(new TraversalRequest(FocusNavigationDirection.First));
|
||||
|
||||
OnClockOpened(new RoutedEventArgs());
|
||||
}
|
||||
@@ -572,7 +574,7 @@ namespace HandyControl.Controls
|
||||
SetCurrentValue(IsDropDownOpenProperty, ValueBoxes.FalseBox);
|
||||
}
|
||||
|
||||
if (_clock.IsKeyboardFocusWithin)
|
||||
if (Clock.IsKeyboardFocusWithin)
|
||||
{
|
||||
MoveFocus(new TraversalRequest(FocusNavigationDirection.First));
|
||||
}
|
||||
|
||||
@@ -192,7 +192,7 @@ namespace HandyControl.Controls
|
||||
/// <summary>
|
||||
/// 当前垂直滚动偏移
|
||||
/// </summary>
|
||||
private static readonly DependencyProperty CurrentVerticalOffsetProperty = DependencyProperty.Register(
|
||||
internal static readonly DependencyProperty CurrentVerticalOffsetProperty = DependencyProperty.Register(
|
||||
"CurrentVerticalOffset", typeof(double), typeof(ScrollViewer), new PropertyMetadata(ValueBoxes.Double0Box, OnCurrentVerticalOffsetChanged));
|
||||
|
||||
private static void OnCurrentVerticalOffsetChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
|
||||
@@ -206,7 +206,7 @@ namespace HandyControl.Controls
|
||||
/// <summary>
|
||||
/// 当前垂直滚动偏移
|
||||
/// </summary>
|
||||
private double CurrentVerticalOffset
|
||||
internal double CurrentVerticalOffset
|
||||
{
|
||||
// ReSharper disable once UnusedMember.Local
|
||||
get => (double) GetValue(CurrentVerticalOffsetProperty);
|
||||
@@ -216,7 +216,7 @@ namespace HandyControl.Controls
|
||||
/// <summary>
|
||||
/// 当前水平滚动偏移
|
||||
/// </summary>
|
||||
public static readonly DependencyProperty CurrentHorizontalOffsetProperty = DependencyProperty.Register(
|
||||
internal static readonly DependencyProperty CurrentHorizontalOffsetProperty = DependencyProperty.Register(
|
||||
"CurrentHorizontalOffset", typeof(double), typeof(ScrollViewer), new PropertyMetadata(ValueBoxes.Double0Box, OnCurrentHorizontalOffsetChanged));
|
||||
|
||||
private static void OnCurrentHorizontalOffsetChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
|
||||
@@ -230,7 +230,7 @@ namespace HandyControl.Controls
|
||||
/// <summary>
|
||||
/// 当前水平滚动偏移
|
||||
/// </summary>
|
||||
public double CurrentHorizontalOffset
|
||||
internal double CurrentHorizontalOffset
|
||||
{
|
||||
get => (double) GetValue(CurrentHorizontalOffsetProperty);
|
||||
set => SetValue(CurrentHorizontalOffsetProperty, value);
|
||||
|
||||
@@ -6,26 +6,23 @@ using System.Windows.Controls.Primitives;
|
||||
using System.Windows.Data;
|
||||
using System.Windows.Input;
|
||||
using System.Windows.Media;
|
||||
using HandyControl.Data;
|
||||
using HandyControl.Tools;
|
||||
|
||||
namespace HandyControl.Controls
|
||||
{
|
||||
[TemplatePart(Name = ElementButtonAm, Type = typeof(RadioButton))]
|
||||
[TemplatePart(Name = ElementButtonPm, Type = typeof(RadioButton))]
|
||||
[TemplatePart(Name = ElementButtonConfirm, Type = typeof(Button))]
|
||||
[TemplatePart(Name = ElementCanvas, Type = typeof(Canvas))]
|
||||
[TemplatePart(Name = ElementBorderTitle, Type = typeof(Border))]
|
||||
[TemplatePart(Name = ElementBorderClock, Type = typeof(Border))]
|
||||
[TemplatePart(Name = ElementPanelNum, Type = typeof(CirclePanel))]
|
||||
[TemplatePart(Name = ElementTimeStr, Type = typeof(TextBlock))]
|
||||
public class Clock : Control
|
||||
public class Clock : ClockBase
|
||||
{
|
||||
#region Constants
|
||||
|
||||
private const string ElementButtonAm = "PART_ButtonAm";
|
||||
private const string ElementButtonPm = "PART_ButtonPm";
|
||||
private const string ElementButtonConfirm = "PART_ButtonConfirm";
|
||||
private const string ElementCanvas = "PART_Canvas";
|
||||
private const string ElementBorderTitle = "PART_BorderTitle";
|
||||
private const string ElementBorderClock = "PART_BorderClock";
|
||||
@@ -40,8 +37,6 @@ namespace HandyControl.Controls
|
||||
|
||||
private RadioButton _buttonPm;
|
||||
|
||||
private Button _buttonConfirm;
|
||||
|
||||
private Canvas _canvas;
|
||||
|
||||
private Border _borderTitle;
|
||||
@@ -60,59 +55,10 @@ namespace HandyControl.Controls
|
||||
|
||||
private int _secValue;
|
||||
|
||||
private bool _appliedTemplate;
|
||||
|
||||
#endregion Data
|
||||
|
||||
#region Public Events
|
||||
|
||||
public static readonly RoutedEvent SelectedTimeChangedEvent =
|
||||
EventManager.RegisterRoutedEvent("SelectedTimeChanged", RoutingStrategy.Direct,
|
||||
typeof(EventHandler<FunctionEventArgs<DateTime?>>), typeof(Clock));
|
||||
|
||||
public event EventHandler<FunctionEventArgs<DateTime?>> SelectedTimeChanged
|
||||
{
|
||||
add => AddHandler(SelectedTimeChangedEvent, value);
|
||||
remove => RemoveHandler(SelectedTimeChangedEvent, value);
|
||||
}
|
||||
|
||||
public event EventHandler<FunctionEventArgs<DateTime>> DisplayTimeChanged;
|
||||
|
||||
public event Action Confirmed;
|
||||
|
||||
#endregion Public Events
|
||||
|
||||
#region Public Properties
|
||||
|
||||
public static readonly DependencyProperty SelectedTimeProperty = DependencyProperty.Register(
|
||||
"SelectedTime", typeof(DateTime?), typeof(Clock), new PropertyMetadata(default(DateTime?), OnSelectedTimeChanged));
|
||||
|
||||
private static void OnSelectedTimeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
|
||||
{
|
||||
var ctl = (Clock)d;
|
||||
var v = (DateTime?)e.NewValue;
|
||||
ctl.DisplayTime = v ?? DateTime.Now;
|
||||
ctl.OnSelectedTimeChanged(new FunctionEventArgs<DateTime?>(SelectedTimeChangedEvent, ctl)
|
||||
{
|
||||
Info = v
|
||||
});
|
||||
}
|
||||
|
||||
public DateTime? SelectedTime
|
||||
{
|
||||
get => (DateTime?) GetValue(SelectedTimeProperty);
|
||||
set => SetValue(SelectedTimeProperty, value);
|
||||
}
|
||||
|
||||
public static readonly DependencyProperty TimeFormatProperty = DependencyProperty.Register(
|
||||
"TimeFormat", typeof(string), typeof(Clock), new PropertyMetadata("HH:mm:ss"));
|
||||
|
||||
public string TimeFormat
|
||||
{
|
||||
get => (string) GetValue(TimeFormatProperty);
|
||||
set => SetValue(TimeFormatProperty, value);
|
||||
}
|
||||
|
||||
public static readonly DependencyProperty ClockRadioButtonStyleProperty = DependencyProperty.Register(
|
||||
"ClockRadioButtonStyle", typeof(Style), typeof(Clock), new PropertyMetadata(default(Style)));
|
||||
|
||||
@@ -122,32 +68,6 @@ namespace HandyControl.Controls
|
||||
set => SetValue(ClockRadioButtonStyleProperty, value);
|
||||
}
|
||||
|
||||
public static readonly DependencyProperty DisplayTimeProperty = DependencyProperty.Register(
|
||||
"DisplayTime", typeof(DateTime), typeof(Clock), new FrameworkPropertyMetadata(DateTime.Now, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnDisplayTimeChanged));
|
||||
|
||||
private static void OnDisplayTimeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
|
||||
{
|
||||
var ctl = (Clock) d;
|
||||
var v = (DateTime) e.NewValue;
|
||||
ctl.Update(v);
|
||||
ctl.OnDisplayTimeChanged(new FunctionEventArgs<DateTime>(v));
|
||||
}
|
||||
|
||||
public DateTime DisplayTime
|
||||
{
|
||||
get => (DateTime) GetValue(DisplayTimeProperty);
|
||||
set => SetValue(DisplayTimeProperty, value);
|
||||
}
|
||||
|
||||
internal static readonly DependencyProperty ShowConfirmButtonProperty = DependencyProperty.Register(
|
||||
"ShowConfirmButton", typeof(bool), typeof(Clock), new PropertyMetadata(ValueBoxes.FalseBox));
|
||||
|
||||
internal bool ShowConfirmButton
|
||||
{
|
||||
get => (bool)GetValue(ShowConfirmButtonProperty);
|
||||
set => SetValue(ShowConfirmButtonProperty, value);
|
||||
}
|
||||
|
||||
private int SecValue
|
||||
{
|
||||
get => _secValue;
|
||||
@@ -174,7 +94,7 @@ namespace HandyControl.Controls
|
||||
|
||||
public override void OnApplyTemplate()
|
||||
{
|
||||
_appliedTemplate = false;
|
||||
AppliedTemplate = false;
|
||||
if (_buttonAm != null)
|
||||
{
|
||||
_buttonAm.Click -= ButtonAm_OnClick;
|
||||
@@ -185,9 +105,9 @@ namespace HandyControl.Controls
|
||||
_buttonPm.Click -= ButtonPm_OnClick;
|
||||
}
|
||||
|
||||
if (_buttonConfirm != null)
|
||||
if (ButtonConfirm != null)
|
||||
{
|
||||
_buttonConfirm.Click -= ButtonConfirm_OnClick;
|
||||
ButtonConfirm.Click -= ButtonConfirm_OnClick;
|
||||
}
|
||||
|
||||
if (_borderTitle != null)
|
||||
@@ -206,18 +126,18 @@ namespace HandyControl.Controls
|
||||
|
||||
_buttonAm = GetTemplateChild(ElementButtonAm) as RadioButton;
|
||||
_buttonPm = GetTemplateChild(ElementButtonPm) as RadioButton;
|
||||
_buttonConfirm = GetTemplateChild(ElementButtonConfirm) as Button;
|
||||
ButtonConfirm = GetTemplateChild(ElementButtonConfirm) as Button;
|
||||
_borderTitle = GetTemplateChild(ElementBorderTitle) as Border;
|
||||
_canvas = GetTemplateChild(ElementCanvas) as Canvas;
|
||||
_borderClock = GetTemplateChild(ElementBorderClock) as Border;
|
||||
_circlePanel = GetTemplateChild(ElementPanelNum) as CirclePanel;
|
||||
_blockTime = GetTemplateChild(ElementTimeStr) as TextBlock;
|
||||
|
||||
CheckNull();
|
||||
if (!CheckNull()) return;
|
||||
|
||||
_buttonAm.Click += ButtonAm_OnClick;
|
||||
_buttonPm.Click += ButtonPm_OnClick;
|
||||
_buttonConfirm.Click += ButtonConfirm_OnClick;
|
||||
ButtonConfirm.Click += ButtonConfirm_OnClick;
|
||||
_borderTitle.MouseWheel += BorderTitle_OnMouseWheel;
|
||||
|
||||
_canvas.MouseWheel += Canvas_OnMouseWheel;
|
||||
@@ -241,7 +161,7 @@ namespace HandyControl.Controls
|
||||
_circlePanel.Children.Add(button);
|
||||
}
|
||||
|
||||
_appliedTemplate = true;
|
||||
AppliedTemplate = true;
|
||||
if (SelectedTime.HasValue)
|
||||
{
|
||||
Update(SelectedTime.Value);
|
||||
@@ -255,26 +175,15 @@ namespace HandyControl.Controls
|
||||
|
||||
#endregion Public Methods
|
||||
|
||||
#region Protected Methods
|
||||
|
||||
protected virtual void OnSelectedTimeChanged(FunctionEventArgs<DateTime?> e) => RaiseEvent(e);
|
||||
|
||||
protected virtual void OnDisplayTimeChanged(FunctionEventArgs<DateTime> e)
|
||||
{
|
||||
var handler = DisplayTimeChanged;
|
||||
handler?.Invoke(this, e);
|
||||
}
|
||||
|
||||
#endregion Protected Methods
|
||||
|
||||
#region Private Methods
|
||||
|
||||
private void CheckNull()
|
||||
private bool CheckNull()
|
||||
{
|
||||
|
||||
if (_buttonPm == null || _buttonAm == null || _buttonConfirm == null || _canvas == null ||
|
||||
if (_buttonPm == null || _buttonAm == null || ButtonConfirm == null || _canvas == null ||
|
||||
_borderTitle == null || _borderClock == null || _circlePanel == null ||
|
||||
_blockTime == null) throw new Exception();
|
||||
_blockTime == null) return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private void BorderTitle_OnMouseWheel(object sender, MouseWheelEventArgs e)
|
||||
@@ -339,7 +248,7 @@ namespace HandyControl.Controls
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (!_appliedTemplate) return;
|
||||
if (!AppliedTemplate) return;
|
||||
var hValue = _currentButton.Num;
|
||||
if (_buttonPm.IsChecked == true)
|
||||
{
|
||||
@@ -367,9 +276,9 @@ namespace HandyControl.Controls
|
||||
/// 更新
|
||||
/// </summary>
|
||||
/// <param name="time"></param>
|
||||
internal void Update(DateTime time)
|
||||
internal override void Update(DateTime time)
|
||||
{
|
||||
if (!_appliedTemplate) return;
|
||||
if (!AppliedTemplate) return;
|
||||
var h = time.Hour;
|
||||
var m = time.Minute;
|
||||
|
||||
@@ -396,12 +305,6 @@ namespace HandyControl.Controls
|
||||
Update();
|
||||
}
|
||||
|
||||
private void ButtonConfirm_OnClick(object sender, RoutedEventArgs e)
|
||||
{
|
||||
SelectedTime = DisplayTime;
|
||||
Confirmed?.Invoke();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取显示时间
|
||||
/// </summary>
|
||||
|
||||
@@ -0,0 +1,106 @@
|
||||
using System;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using HandyControl.Data;
|
||||
|
||||
namespace HandyControl.Controls
|
||||
{
|
||||
[TemplatePart(Name = ElementButtonConfirm, Type = typeof(Button))]
|
||||
public abstract class ClockBase : Control
|
||||
{
|
||||
protected const string ElementButtonConfirm = "PART_ButtonConfirm";
|
||||
|
||||
protected Button ButtonConfirm;
|
||||
|
||||
protected bool AppliedTemplate;
|
||||
|
||||
public event Action Confirmed;
|
||||
|
||||
public event EventHandler<FunctionEventArgs<DateTime>> DisplayTimeChanged;
|
||||
|
||||
public static readonly RoutedEvent SelectedTimeChangedEvent =
|
||||
EventManager.RegisterRoutedEvent("SelectedTimeChanged", RoutingStrategy.Direct,
|
||||
typeof(EventHandler<FunctionEventArgs<DateTime?>>), typeof(ClockBase));
|
||||
|
||||
public event EventHandler<FunctionEventArgs<DateTime?>> SelectedTimeChanged
|
||||
{
|
||||
add => AddHandler(SelectedTimeChangedEvent, value);
|
||||
remove => RemoveHandler(SelectedTimeChangedEvent, value);
|
||||
}
|
||||
|
||||
public static readonly DependencyProperty TimeFormatProperty = DependencyProperty.Register(
|
||||
"TimeFormat", typeof(string), typeof(ClockBase), new PropertyMetadata("HH:mm:ss"));
|
||||
|
||||
public string TimeFormat
|
||||
{
|
||||
get => (string)GetValue(TimeFormatProperty);
|
||||
set => SetValue(TimeFormatProperty, value);
|
||||
}
|
||||
|
||||
public static readonly DependencyProperty SelectedTimeProperty = DependencyProperty.Register(
|
||||
"SelectedTime", typeof(DateTime?), typeof(ClockBase), new PropertyMetadata(default(DateTime?), OnSelectedTimeChanged));
|
||||
|
||||
private static void OnSelectedTimeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
|
||||
{
|
||||
var ctl = (ClockBase)d;
|
||||
var v = (DateTime?)e.NewValue;
|
||||
ctl.DisplayTime = v ?? DateTime.Now;
|
||||
ctl.OnSelectedTimeChanged(new FunctionEventArgs<DateTime?>(SelectedTimeChangedEvent, ctl)
|
||||
{
|
||||
Info = v
|
||||
});
|
||||
}
|
||||
|
||||
public DateTime? SelectedTime
|
||||
{
|
||||
get => (DateTime?) GetValue(SelectedTimeProperty);
|
||||
set => SetValue(SelectedTimeProperty, value);
|
||||
}
|
||||
|
||||
public static readonly DependencyProperty DisplayTimeProperty = DependencyProperty.Register(
|
||||
"DisplayTime", typeof(DateTime), typeof(ClockBase),
|
||||
new FrameworkPropertyMetadata(DateTime.Now, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
|
||||
OnDisplayTimeChanged));
|
||||
|
||||
private static void OnDisplayTimeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
|
||||
{
|
||||
var ctl = (ClockBase)d;
|
||||
var v = (DateTime)e.NewValue;
|
||||
ctl.Update(v);
|
||||
ctl.OnDisplayTimeChanged(new FunctionEventArgs<DateTime>(v));
|
||||
}
|
||||
|
||||
public DateTime DisplayTime
|
||||
{
|
||||
get => (DateTime)GetValue(DisplayTimeProperty);
|
||||
set => SetValue(DisplayTimeProperty, value);
|
||||
}
|
||||
|
||||
internal static readonly DependencyProperty ShowConfirmButtonProperty = DependencyProperty.Register(
|
||||
"ShowConfirmButton", typeof(bool), typeof(ClockBase), new PropertyMetadata(ValueBoxes.FalseBox));
|
||||
|
||||
internal bool ShowConfirmButton
|
||||
{
|
||||
get => (bool) GetValue(ShowConfirmButtonProperty);
|
||||
set => SetValue(ShowConfirmButtonProperty, value);
|
||||
}
|
||||
|
||||
protected virtual void OnSelectedTimeChanged(FunctionEventArgs<DateTime?> e) => RaiseEvent(e);
|
||||
|
||||
protected virtual void OnDisplayTimeChanged(FunctionEventArgs<DateTime> e)
|
||||
{
|
||||
var handler = DisplayTimeChanged;
|
||||
handler?.Invoke(this, e);
|
||||
}
|
||||
|
||||
protected void ButtonConfirm_OnClick(object sender, RoutedEventArgs e)
|
||||
{
|
||||
SelectedTime = DisplayTime;
|
||||
Confirmed?.Invoke();
|
||||
}
|
||||
|
||||
internal abstract void Update(DateTime time);
|
||||
|
||||
protected void Clock_SelectedTimeChanged(object sender, FunctionEventArgs<DateTime?> e) => SelectedTime = e.Info;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,148 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
|
||||
namespace HandyControl.Controls
|
||||
{
|
||||
[TemplatePart(Name = ElementHourList, Type = typeof(ListBox))]
|
||||
[TemplatePart(Name = ElementMinuteList, Type = typeof(ListBox))]
|
||||
[TemplatePart(Name = ElementSecondList, Type = typeof(ListBox))]
|
||||
public class ListClock : ClockBase
|
||||
{
|
||||
#region Constants
|
||||
|
||||
private const string ElementHourList = "PART_HourList";
|
||||
private const string ElementMinuteList = "PART_MinuteList";
|
||||
private const string ElementSecondList = "PART_SecondList";
|
||||
|
||||
#endregion Constants
|
||||
|
||||
#region Data
|
||||
|
||||
private ListBox _hourList;
|
||||
|
||||
private ListBox _minuteList;
|
||||
|
||||
private ListBox _secondList;
|
||||
|
||||
#endregion Data
|
||||
|
||||
public override void OnApplyTemplate()
|
||||
{
|
||||
AppliedTemplate = false;
|
||||
if (ButtonConfirm != null)
|
||||
{
|
||||
ButtonConfirm.Click -= ButtonConfirm_OnClick;
|
||||
}
|
||||
|
||||
if (_hourList != null)
|
||||
{
|
||||
_hourList.SelectionChanged -= HourList_SelectionChanged;
|
||||
}
|
||||
|
||||
if (_minuteList != null)
|
||||
{
|
||||
_minuteList.SelectionChanged -= MinuteList_SelectionChanged;
|
||||
}
|
||||
|
||||
if (_secondList != null)
|
||||
{
|
||||
_secondList.SelectionChanged -= SecondList_SelectionChanged;
|
||||
}
|
||||
|
||||
base.OnApplyTemplate();
|
||||
|
||||
_hourList = GetTemplateChild(ElementHourList) as ListBox;
|
||||
if (_hourList != null)
|
||||
{
|
||||
CreateItemsSource(_hourList, 24);
|
||||
_hourList.SelectionChanged += HourList_SelectionChanged;
|
||||
}
|
||||
|
||||
_minuteList = GetTemplateChild(ElementMinuteList) as ListBox;
|
||||
if (_minuteList != null)
|
||||
{
|
||||
CreateItemsSource(_minuteList, 60);
|
||||
_minuteList.SelectionChanged += MinuteList_SelectionChanged;
|
||||
}
|
||||
|
||||
_secondList = GetTemplateChild(ElementSecondList) as ListBox;
|
||||
if (_secondList != null)
|
||||
{
|
||||
CreateItemsSource(_secondList, 60);
|
||||
_secondList.SelectionChanged += SecondList_SelectionChanged;
|
||||
}
|
||||
|
||||
ButtonConfirm = GetTemplateChild(ElementButtonConfirm) as Button;
|
||||
|
||||
if (ButtonConfirm != null)
|
||||
{
|
||||
ButtonConfirm.Click += ButtonConfirm_OnClick;
|
||||
}
|
||||
|
||||
AppliedTemplate = true;
|
||||
if (SelectedTime.HasValue)
|
||||
{
|
||||
Update(SelectedTime.Value);
|
||||
}
|
||||
else
|
||||
{
|
||||
DisplayTime = DateTime.Now;
|
||||
Update(DisplayTime);
|
||||
}
|
||||
}
|
||||
|
||||
private void HourList_SelectionChanged(object sender, SelectionChangedEventArgs e) => Update();
|
||||
|
||||
private void MinuteList_SelectionChanged(object sender, SelectionChangedEventArgs e) => Update();
|
||||
|
||||
private void SecondList_SelectionChanged(object sender, SelectionChangedEventArgs e) => Update();
|
||||
|
||||
private void CreateItemsSource(ItemsControl selector, int count)
|
||||
{
|
||||
var list = new List<string>();
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
list.Add(i.ToString("#00"));
|
||||
}
|
||||
|
||||
selector.ItemsSource = list;
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (_hourList.SelectedIndex >= 0 && _hourList.SelectedIndex < 24 &&
|
||||
_minuteList.SelectedIndex >= 0 && _minuteList.SelectedIndex < 60 &&
|
||||
_secondList.SelectedIndex >= 0 && _secondList.SelectedIndex < 60)
|
||||
{
|
||||
var now = DateTime.Now;
|
||||
DisplayTime = new DateTime(now.Year, now.Month, now.Day, _hourList.SelectedIndex,
|
||||
_minuteList.SelectedIndex, _secondList.SelectedIndex);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 更新
|
||||
/// </summary>
|
||||
/// <param name="time"></param>
|
||||
internal override void Update(DateTime time)
|
||||
{
|
||||
if (!AppliedTemplate) return;
|
||||
|
||||
var h = time.Hour;
|
||||
var m = time.Minute;
|
||||
var s = time.Second;
|
||||
|
||||
_hourList.SelectedIndex = h;
|
||||
_minuteList.SelectedIndex = m;
|
||||
_secondList.SelectedIndex = s;
|
||||
|
||||
_hourList.ScrollIntoView(_hourList.SelectedItem);
|
||||
_minuteList.ScrollIntoView(_minuteList.SelectedItem);
|
||||
_secondList.ScrollIntoView(_secondList.SelectedItem);
|
||||
|
||||
DisplayTime = time;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user