| 1 | using System; |
| 2 | using System.Collections.Generic; |
| 3 | using System.Linq; |
| 4 | using System.Text; |
| 5 | using System.Threading.Tasks; |
| 6 | using System.Windows; |
| 7 | using System.Windows.Controls; |
| 8 | using System.Windows.Input; |
| 9 | |
| 10 | namespace UI.Controls.Toggle |
| 11 | { |
| 12 | public class Toggle : Control |
| 13 | { |
| 14 | public event EventHandler ToggleChanged; |
| 15 | public bool IsChecked |
| 16 | { |
| 17 | get { return (bool)GetValue(IsCheckedProperty); } |
| 18 | set { SetValue(IsCheckedProperty, value); } |
| 19 | } |
| 20 | public static readonly DependencyProperty IsCheckedProperty = |
| 21 | DependencyProperty.Register("IsChecked", typeof(bool), typeof(Toggle), new FrameworkPropertyMetadata(true, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, new PropertyChangedCallback(OnIsCheckedChanged))); |
| 22 | |
| 23 | private static void OnIsCheckedChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) |
| 24 | { |
| 25 | var control = d as Toggle; |
| 26 | |
| 27 | VisualStateManager.GoToState(control, control.IsChecked ? "On" : "Off", true); |
| 28 | } |
| 29 | public ToggleTextPosition TextPosition |
| 30 | { |
| 31 | get { return (ToggleTextPosition)GetValue(TextPositionProperty); } |
| 32 | set { SetValue(TextPositionProperty, value); } |
| 33 | } |
| 34 | public static readonly DependencyProperty TextPositionProperty = |
| 35 | DependencyProperty.Register("TextPosition", typeof(ToggleTextPosition), typeof(Toggle), new PropertyMetadata(ToggleTextPosition.Right)); |
| 36 | public string OnText |
| 37 | { |
| 38 | get { return (string)GetValue(OnTextProperty); } |
| 39 | set { SetValue(OnTextProperty, value); } |
| 40 | } |
| 41 | public static readonly DependencyProperty OnTextProperty = |
| 42 | DependencyProperty.Register("OnText", typeof(string), typeof(Toggle), new PropertyMetadata("开")); |
| 43 | public string OffText |
| 44 | { |
| 45 | get { return (string)GetValue(OffTextProperty); } |
| 46 | set { SetValue(OffTextProperty, value); } |
| 47 | } |
| 48 | public static readonly DependencyProperty OffTextProperty = |
| 49 | DependencyProperty.Register("OffText", typeof(string), typeof(Toggle), new PropertyMetadata("关")); |
| 50 | public string Text |
| 51 | { |
| 52 | get { return (string)GetValue(TextProperty); } |
| 53 | set { SetValue(TextProperty, value); } |
| 54 | } |
| 55 | public static readonly DependencyProperty TextProperty = |
| 56 | DependencyProperty.Register("Text", typeof(string), typeof(Toggle)); |
| 57 | |
| 58 | public Toggle() |
| 59 | { |
| 60 | DefaultStyleKey = typeof(Toggle); |
| 61 | |
| 62 | } |
| 63 | |
| 64 | public override void OnApplyTemplate() |
| 65 | { |
| 66 | base.OnApplyTemplate(); |
| 67 | } |
| 68 | |
| 69 | protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e) |
| 70 | { |
| 71 | base.OnMouseLeftButtonUp(e); |
| 72 | IsChecked = !IsChecked; |
| 73 | ToggleChanged?.Invoke(this, EventArgs.Empty); |
| 74 | } |
| 75 | |
| 76 | private void Toggle_Loaded(object sender, RoutedEventArgs e) |
| 77 | { |
| 78 | |
| 79 | } |
| 80 | } |
| 81 | } |