Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

135 wiersze
4.5 KiB

  1. using System;
  2. using System.Windows;
  3. using System.Windows.Controls;
  4. using System.Windows.Controls.Primitives;
  5. using System.Windows.Media.Animation;
  6. namespace BPA.UIControl
  7. {
  8. /// <summary>
  9. /// ToggleButton 帮助类
  10. /// </summary>
  11. public class ToggleButtonHelper
  12. {
  13. /// <summary>
  14. /// Unchecked 显示内容,内部使用
  15. /// </summary>
  16. internal static readonly DependencyPropertyKey UncheckedContentPropertyKey = DependencyProperty.RegisterAttachedReadOnly(
  17. "UncheckedContent", typeof(object), typeof(ToggleButtonHelper), new PropertyMetadata(default(object)));
  18. /// <summary>
  19. /// Unchecked 显示内容,内部使用
  20. /// </summary>
  21. public static readonly DependencyProperty UncheckedContentProperty = UncheckedContentPropertyKey.DependencyProperty;
  22. private static void SetUncheckedContent(DependencyObject element, object value)
  23. {
  24. element.SetValue(UncheckedContentPropertyKey, value);
  25. }
  26. private static object GetUncheckedContent(DependencyObject element)
  27. {
  28. return (object)element.GetValue(UncheckedContentProperty);
  29. }
  30. /// <summary>
  31. /// Checked 显示内容
  32. /// </summary>
  33. public static readonly DependencyProperty CheckedContentProperty = DependencyProperty.RegisterAttached(
  34. "CheckedContent", typeof(object), typeof(ToggleButtonHelper), new PropertyMetadata(default(object), OnCheckedContentChanged));
  35. /// <summary>
  36. /// Sets the checked content.
  37. /// </summary>
  38. /// <param name="element">The element.</param>
  39. /// <param name="value">The value.</param>
  40. public static void SetCheckedContent(DependencyObject element, object value)
  41. {
  42. element.SetValue(CheckedContentProperty, value);
  43. }
  44. /// <summary>
  45. /// Gets the checked content.
  46. /// </summary>
  47. /// <param name="element">The element.</param>
  48. /// <returns>An object.</returns>
  49. public static object GetCheckedContent(DependencyObject element)
  50. {
  51. return (object)element.GetValue(CheckedContentProperty);
  52. }
  53. private static void OnCheckedContentChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  54. {
  55. if (d is ToggleButton toggleButton)
  56. {
  57. if (toggleButton.IsLoaded)
  58. {
  59. AddCheckedEvents(toggleButton);
  60. }
  61. else
  62. {
  63. toggleButton.Loaded += ToggleButton_Loaded;
  64. }
  65. }
  66. }
  67. private static void AddCheckedEvents(ToggleButton toggleButton)
  68. {
  69. if (toggleButton.Content != null)
  70. {
  71. SetUncheckedContent(toggleButton, toggleButton.Content);
  72. }
  73. if (toggleButton.IsChecked == true)
  74. {
  75. CheckAction(toggleButton);
  76. }
  77. toggleButton.Checked -= ToggleButton_Checked;
  78. toggleButton.Unchecked -= ToggleButton_Checked;
  79. toggleButton.Indeterminate -= ToggleButton_Checked;
  80. if (GetCheckedContent(toggleButton) != null)
  81. {
  82. toggleButton.Checked += ToggleButton_Checked;
  83. toggleButton.Unchecked += ToggleButton_Checked;
  84. toggleButton.Indeterminate += ToggleButton_Checked;
  85. }
  86. }
  87. private static void ToggleButton_Loaded(object sender, RoutedEventArgs e)
  88. {
  89. var toggleButton = sender as ToggleButton;
  90. AddCheckedEvents(toggleButton);
  91. toggleButton.Loaded -= ToggleButton_Loaded;
  92. }
  93. private static void ToggleButton_Checked(object sender, RoutedEventArgs e)
  94. {
  95. var toggleButton = (ToggleButton)sender;
  96. if (toggleButton.IsChecked == false)
  97. {
  98. UncheckAction(toggleButton);
  99. }
  100. else
  101. {
  102. CheckAction(toggleButton);
  103. }
  104. }
  105. private static void CheckAction(ToggleButton toggleButton)
  106. {
  107. if (GetUncheckedContent(toggleButton) == null)
  108. {
  109. SetUncheckedContent(toggleButton, toggleButton.Content);
  110. }
  111. toggleButton.Content = GetCheckedContent(toggleButton);
  112. }
  113. private static void UncheckAction(ToggleButton toggleButton)
  114. {
  115. toggleButton.Content = GetUncheckedContent(toggleButton);
  116. }
  117. }
  118. }