using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace NEIntelligentControl2.Views.AGC { /// /// 小圆点页面 /// public partial class DotView : UserControl { // ------------事件-------------- public static readonly RoutedEvent ChangedEvent = EventManager.RegisterRoutedEvent("Changed", RoutingStrategy.Bubble, typeof(EventHandler), typeof(DotView)); public event RoutedEventHandler Changed { add { AddHandler(ChangedEvent, value); } remove { RemoveHandler(ChangedEvent, value); } } private Dictionary _ButtonPairs;// 小圆点列表 private int count; /// /// 小圆点数量 /// public int Count { get => count; set { count = value; _ButtonPairs = new Dictionary(); _SPMain.Children.Clear(); var style = this.Resources["MainRadioButtonStyle"] as Style; for (int i = 0; i < count; ++i) { RadioButton rb = new RadioButton() { Style = style }; rb.Checked += DotChecked; _ButtonPairs.Add(rb, i); _SPMain.Children.Add(rb); } } } private void DotChecked(object sender, RoutedEventArgs e) { RadioButton rb = sender as RadioButton; if (rb == null) return; DotChangedRoutedEventArgs dcre = new DotChangedRoutedEventArgs(ChangedEvent, sender, this[rb]); RaiseEvent(dcre); } /// /// 选择小圆点 /// /// 小圆点序列(从0开始) /// 是否成功 public bool Check(int index) { foreach (var v in _ButtonPairs) { if (v.Value == index) { v.Key.IsChecked = true; return true; } } return false; } /// /// 获取选中的按钮序列 /// /// 按钮 /// 序列 public int this[RadioButton rb] { get { if (_ButtonPairs.ContainsKey(rb)) return _ButtonPairs[rb]; return -1; } } public DotView() { _ButtonPairs = new Dictionary(); InitializeComponent(); } } /// /// 小圆点选择改变你事件 /// public class DotChangedRoutedEventArgs : RoutedEventArgs { /// /// 选择的小圆点序列 /// public int Index { get; set; } /// /// 选择的按钮 /// public RadioButton SelectedButton { get; set; } public DotChangedRoutedEventArgs(RoutedEvent routedEvent, object source, int index) : base(routedEvent, source) { Index = index; SelectedButton = source as RadioButton; } } }