DotView.xaml.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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.Data;
  9. using System.Windows.Documents;
  10. using System.Windows.Input;
  11. using System.Windows.Media;
  12. using System.Windows.Media.Imaging;
  13. using System.Windows.Navigation;
  14. using System.Windows.Shapes;
  15. namespace NEIntelligentControl2.Views.AGC
  16. {
  17. /// <summary>
  18. /// 小圆点页面
  19. /// </summary>
  20. public partial class DotView : UserControl
  21. {
  22. // ------------事件--------------
  23. public static readonly RoutedEvent ChangedEvent = EventManager.RegisterRoutedEvent("Changed", RoutingStrategy.Bubble, typeof(EventHandler<DotChangedRoutedEventArgs>), typeof(DotView));
  24. public event RoutedEventHandler Changed { add { AddHandler(ChangedEvent, value); } remove { RemoveHandler(ChangedEvent, value); } }
  25. private Dictionary<RadioButton, int> _ButtonPairs;// 小圆点列表
  26. private int count;
  27. /// <summary>
  28. /// 小圆点数量
  29. /// </summary>
  30. public int Count
  31. {
  32. get => count;
  33. set
  34. {
  35. count = value;
  36. _ButtonPairs = new Dictionary<RadioButton, int>();
  37. _SPMain.Children.Clear();
  38. var style = this.Resources["MainRadioButtonStyle"] as Style;
  39. for (int i = 0; i < count; ++i)
  40. {
  41. RadioButton rb = new RadioButton() { Style = style };
  42. rb.Checked += DotChecked;
  43. _ButtonPairs.Add(rb, i);
  44. _SPMain.Children.Add(rb);
  45. }
  46. }
  47. }
  48. private void DotChecked(object sender, RoutedEventArgs e)
  49. {
  50. RadioButton rb = sender as RadioButton;
  51. if (rb == null) return;
  52. DotChangedRoutedEventArgs dcre = new DotChangedRoutedEventArgs(ChangedEvent, sender, this[rb]);
  53. RaiseEvent(dcre);
  54. }
  55. /// <summary>
  56. /// 选择小圆点
  57. /// </summary>
  58. /// <param name="index">小圆点序列(从0开始)</param>
  59. /// <returns>是否成功</returns>
  60. public bool Check(int index)
  61. {
  62. foreach (var v in _ButtonPairs)
  63. {
  64. if (v.Value == index)
  65. {
  66. v.Key.IsChecked = true;
  67. return true;
  68. }
  69. }
  70. return false;
  71. }
  72. /// <summary>
  73. /// 获取选中的按钮序列
  74. /// </summary>
  75. /// <param name="rb">按钮</param>
  76. /// <returns>序列</returns>
  77. public int this[RadioButton rb]
  78. {
  79. get
  80. {
  81. if (_ButtonPairs.ContainsKey(rb))
  82. return _ButtonPairs[rb];
  83. return -1;
  84. }
  85. }
  86. public DotView()
  87. {
  88. _ButtonPairs = new Dictionary<RadioButton, int>();
  89. InitializeComponent();
  90. }
  91. }
  92. /// <summary>
  93. /// 小圆点选择改变你事件
  94. /// </summary>
  95. public class DotChangedRoutedEventArgs : RoutedEventArgs
  96. {
  97. /// <summary>
  98. /// 选择的小圆点序列
  99. /// </summary>
  100. public int Index { get; set; }
  101. /// <summary>
  102. /// 选择的按钮
  103. /// </summary>
  104. public RadioButton SelectedButton { get; set; }
  105. public DotChangedRoutedEventArgs(RoutedEvent routedEvent, object source, int index) : base(routedEvent, source)
  106. {
  107. Index = index;
  108. SelectedButton = source as RadioButton;
  109. }
  110. }
  111. }