跳到主要内容

github/tai

公开

Tai · Windows 软件与网站使用时长统计。上游 Planshit/Tai 的源码镜像,非灵栈原创;MIT 许可。来源:https://github.com/Planshit/Tai 。本次为手动同步,暂不提供安装包。

浏览文件

Select.cs

136 行 · 4.6 KB历史下载
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Threading.Tasks;
6using System.Windows;
7using System.Windows.Controls;
8
9namespace UI.Controls.Select
10{
11 public class Select : Control
12 {
13 public bool IsShowIcon
14 {
15 get { return (bool)GetValue(IsShowIconProperty); }
16 set { SetValue(IsShowIconProperty, value); }
17 }
18 public static readonly DependencyProperty IsShowIconProperty =
19 DependencyProperty.Register("IsShowIcon",
20 typeof(bool),
21 typeof(Select), new PropertyMetadata(true));
22 public bool IsOpen
23 {
24 get { return (bool)GetValue(IsOpenProperty); }
25 set { SetValue(IsOpenProperty, value); }
26 }
27 public static readonly DependencyProperty IsOpenProperty =
28 DependencyProperty.Register("IsOpen",
29 typeof(bool),
30 typeof(Select), new PropertyMetadata(new PropertyChangedCallback(OnIsOpenChanged)));
31
32 private static void OnIsOpenChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
33 {
34 var control = d as Select;
35 if (control != null)
36 {
37 VisualStateManager.GoToState(control, control.IsOpen ? "ShowPopup" : "Normal", true);
38 }
39 }
40
41 public List<SelectItemModel> Options
42 {
43 get { return (List<SelectItemModel>)GetValue(OptionsProperty); }
44 set { SetValue(OptionsProperty, value); }
45 }
46 public static readonly DependencyProperty OptionsProperty =
47 DependencyProperty.Register("Options",
48 typeof(List<SelectItemModel>),
49 typeof(Select), new PropertyMetadata(new PropertyChangedCallback(OnPropertyChanged)));
50
51 private static void OnPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
52 {
53 var control = (d as Select);
54 if (e.Property == OptionsProperty && e.OldValue != e.NewValue)
55 {
56 control.RenderOptions();
57 }
58 if (e.Property == SelectItemProperty && e.NewValue != e.OldValue)
59 {
60 control.OnSelectedItemChange();
61 }
62 }
63
64 public SelectItemModel SelectedItem
65 {
66 get { return (SelectItemModel)GetValue(SelectItemProperty); }
67 set { SetValue(SelectItemProperty, value); }
68 }
69
70 public static readonly DependencyProperty SelectItemProperty = DependencyProperty.Register("SelectedItem", typeof(SelectItemModel), typeof(Select), new PropertyMetadata(new PropertyChangedCallback(OnPropertyChanged)));
71
72 public event EventHandler OnSelectedItemChanged;
73
74 private StackPanel _optionsContainer;
75 public Select()
76 {
77 DefaultStyleKey = typeof(Select);
78 MouseLeftButtonUp += Select_MouseLeftButtonUp;
79 }
80
81 private void Select_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
82 {
83 IsOpen = !IsOpen;
84 }
85
86 public override void OnApplyTemplate()
87 {
88 base.OnApplyTemplate();
89 _optionsContainer = GetTemplateChild("OptionsContainer") as StackPanel;
90 this.RenderOptions();
91 }
92
93 public void RenderOptions()
94 {
95 if (_optionsContainer == null || Options == null)
96 {
97 return;
98 }
99
100 _optionsContainer?.Children.Clear();
101
102 foreach (var item in Options)
103 {
104 var option = new Option();
105 option.Value = item;
106 option.IsShowIcon = IsShowIcon;
107 option.IsChecked = SelectedItem?.Name == item.Name;
108 option.MouseLeftButtonUp += Option_MouseLeftButtonUp;
109 _optionsContainer.Children.Add(option);
110 }
111 }
112
113 private void Option_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
114 {
115 var option = sender as Option;
116 if (option != null)
117 {
118 SelectedItem = option.Value;
119 OnSelectedItemChange();
120 OnSelectedItemChanged?.Invoke(this, EventArgs.Empty);
121 }
122 }
123
124 private void OnSelectedItemChange()
125 {
126 if (_optionsContainer == null || SelectedItem == null)
127 {
128 return;
129 }
130 foreach (Option option in _optionsContainer.Children)
131 {
132 option.IsChecked = SelectedItem.Name == option.Value.Name;
133 }
134 }
135 }
136}