PageRenderTime 944ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/MahApps.Metro/Controls/ToggleSwitch.cs

https://github.com/HansHuang/DownloadPic
C# | 198 lines | 161 code | 33 blank | 4 comment | 17 complexity | f1fcacff05b3f8fd2bfd96829cdeed8e MD5 | raw file
  1. // (c) Copyright Microsoft Corporation.
  2. // This source is subject to the Microsoft Public License (Ms-PL).
  3. // Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
  4. // All other rights reserved.
  5. using System;
  6. using System.ComponentModel;
  7. using System.Globalization;
  8. using System.Windows;
  9. using System.Windows.Controls;
  10. using System.Windows.Controls.Primitives;
  11. using System.Windows.Data;
  12. using System.Windows.Media;
  13. using MahApps.Metro.Converters;
  14. namespace MahApps.Metro.Controls
  15. {
  16. [TemplateVisualState(Name = NormalState, GroupName = CommonStates)]
  17. [TemplateVisualState(Name = DisabledState, GroupName = CommonStates)]
  18. [TemplatePart(Name = SwitchPart, Type = typeof(ToggleButton))]
  19. public class ToggleSwitch : ContentControl
  20. {
  21. private const string CommonStates = "CommonStates";
  22. private const string NormalState = "Normal";
  23. private const string DisabledState = "Disabled";
  24. private const string SwitchPart = "Switch";
  25. private ToggleButton _toggleButton;
  26. private bool _wasContentSet;
  27. public static readonly DependencyProperty OnProperty = DependencyProperty.Register("OnLabel", typeof(string), typeof(ToggleSwitch), new PropertyMetadata("On"));
  28. public static readonly DependencyProperty OffProperty = DependencyProperty.Register("OffLabel", typeof(string), typeof(ToggleSwitch), new PropertyMetadata("Off"));
  29. public static readonly DependencyProperty HeaderProperty = DependencyProperty.Register("Header", typeof(object), typeof(ToggleSwitch), new PropertyMetadata(null));
  30. public static readonly DependencyProperty HeaderTemplateProperty = DependencyProperty.Register("HeaderTemplate", typeof(DataTemplate), typeof(ToggleSwitch), new PropertyMetadata(null));
  31. public static readonly DependencyProperty SwitchForegroundProperty = DependencyProperty.Register("SwitchForeground", typeof(Brush), typeof(ToggleSwitch), null);
  32. public static readonly DependencyProperty IsCheckedProperty = DependencyProperty.Register("IsChecked", typeof(bool?), typeof(ToggleSwitch), new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnIsCheckedChanged));
  33. public event EventHandler<RoutedEventArgs> Checked;
  34. public event EventHandler<RoutedEventArgs> Unchecked;
  35. public event EventHandler<RoutedEventArgs> Indeterminate;
  36. public event EventHandler<RoutedEventArgs> Click;
  37. public string OnLabel
  38. {
  39. get { return (string)GetValue(OnProperty); }
  40. set { SetValue(OnProperty, value); }
  41. }
  42. public string OffLabel
  43. {
  44. get { return (string)GetValue(OffProperty); }
  45. set { SetValue(OffProperty, value); }
  46. }
  47. public object Header
  48. {
  49. get { return GetValue(HeaderProperty); }
  50. set { SetValue(HeaderProperty, value); }
  51. }
  52. public DataTemplate HeaderTemplate
  53. {
  54. get { return (DataTemplate)GetValue(HeaderTemplateProperty); }
  55. set { SetValue(HeaderTemplateProperty, value); }
  56. }
  57. public Brush SwitchForeground
  58. {
  59. get { return (Brush)GetValue(SwitchForegroundProperty); }
  60. set
  61. {
  62. SetValue(SwitchForegroundProperty, value);
  63. }
  64. }
  65. [TypeConverter(typeof(NullableBoolConverter))]
  66. public bool? IsChecked
  67. {
  68. get { return (bool?)GetValue(IsCheckedProperty); }
  69. set { SetValue(IsCheckedProperty, value); }
  70. }
  71. public event EventHandler IsCheckedChanged;
  72. private static void OnIsCheckedChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  73. {
  74. var toggleSwitch = (ToggleSwitch)d;
  75. if (toggleSwitch._toggleButton != null)
  76. {
  77. var oldValue = (bool?)e.OldValue;
  78. var newValue = (bool?)e.NewValue;
  79. toggleSwitch._toggleButton.IsChecked = newValue;
  80. if (oldValue != newValue
  81. && toggleSwitch.IsCheckedChanged != null)
  82. {
  83. toggleSwitch.IsCheckedChanged(toggleSwitch, EventArgs.Empty);
  84. }
  85. }
  86. }
  87. public ToggleSwitch()
  88. {
  89. DefaultStyleKey = typeof(ToggleSwitch);
  90. PreviewKeyUp += ToggleSwitch_PreviewKeyUp;
  91. }
  92. void ToggleSwitch_PreviewKeyUp(object sender, System.Windows.Input.KeyEventArgs e)
  93. {
  94. if (e.Key == System.Windows.Input.Key.Space && e.OriginalSource == sender)
  95. IsChecked = !IsChecked;
  96. }
  97. private void SetDefaultContent()
  98. {
  99. Binding binding = new Binding("IsChecked") { Source = this, Converter = new OffOnConverter(), ConverterParameter = this };
  100. SetBinding(ContentProperty, binding);
  101. }
  102. private void ChangeVisualState(bool useTransitions)
  103. {
  104. VisualStateManager.GoToState(this, IsEnabled ? NormalState : DisabledState, useTransitions);
  105. }
  106. protected override void OnContentChanged(object oldContent, object newContent)
  107. {
  108. base.OnContentChanged(oldContent, newContent);
  109. _wasContentSet = true;
  110. }
  111. public override void OnApplyTemplate()
  112. {
  113. base.OnApplyTemplate();
  114. if (!_wasContentSet && GetBindingExpression(ContentProperty) == null)
  115. {
  116. SetDefaultContent();
  117. }
  118. if (_toggleButton != null)
  119. {
  120. _toggleButton.Checked -= CheckedHandler;
  121. _toggleButton.Unchecked -= UncheckedHandler;
  122. _toggleButton.Indeterminate -= IndeterminateHandler;
  123. _toggleButton.Click -= ClickHandler;
  124. }
  125. _toggleButton = GetTemplateChild(SwitchPart) as ToggleButton;
  126. if (_toggleButton != null)
  127. {
  128. _toggleButton.Checked += CheckedHandler;
  129. _toggleButton.Unchecked += UncheckedHandler;
  130. _toggleButton.Indeterminate += IndeterminateHandler;
  131. _toggleButton.Click += ClickHandler;
  132. _toggleButton.IsChecked = IsChecked;
  133. }
  134. ChangeVisualState(false);
  135. }
  136. private void CheckedHandler(object sender, RoutedEventArgs e)
  137. {
  138. IsChecked = true;
  139. SafeRaise.Raise(Checked, this, e);
  140. }
  141. private void UncheckedHandler(object sender, RoutedEventArgs e)
  142. {
  143. IsChecked = false;
  144. SafeRaise.Raise(Unchecked, this, e);
  145. }
  146. private void IndeterminateHandler(object sender, RoutedEventArgs e)
  147. {
  148. IsChecked = null;
  149. SafeRaise.Raise(Indeterminate, this, e);
  150. }
  151. private void ClickHandler(object sender, RoutedEventArgs e)
  152. {
  153. SafeRaise.Raise(Click, this, e);
  154. }
  155. public override string ToString()
  156. {
  157. return string.Format(
  158. CultureInfo.InvariantCulture,
  159. "{{ToggleSwitch IsChecked={0}, Content={1}}}",
  160. IsChecked,
  161. Content
  162. );
  163. }
  164. }
  165. }