| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- using System.Collections;
- using System.Windows;
- using HandyControl.Controls;
- namespace TeamAAS.Dialogs.Dialogs
- {
- /// <summary>
- /// 选择对话框,用于让用户从列表中选择一项。
- /// 通过 DialogHelper.Select 调用。
- /// </summary>
- public partial class SelectDialog
- {
- /// <summary>用户选中的项,点击确定后有效;未选择为 null</summary>
- public object SelectedItem { get; private set; }
- /// <summary>用户是否点击了确定</summary>
- public bool Result { get; private set; }
- public SelectDialog()
- {
- InitializeComponent();
- UiScaler.Attach(this); // 分辨率/DPI 自适应
- }
- /// <summary>
- /// 配置选择对话框。
- /// </summary>
- /// <param name="label">提示文本</param>
- /// <param name="items">可选项列表</param>
- /// <param name="title">窗口标题</param>
- /// <param name="defaultItem">默认选中项</param>
- public void Setup(string label, IEnumerable items, string title = "选择", object defaultItem = null)
- {
- Title = title;
- LabelText.Text = label;
- SelectBox.Items.Clear();
- foreach (var item in items)
- {
- SelectBox.Items.Add(item);
- }
- if (defaultItem != null)
- {
- SelectBox.SelectedItem = defaultItem;
- }
- else if (SelectBox.Items.Count > 0)
- {
- SelectBox.SelectedIndex = 0;
- }
- }
- private void BtnOK_Click(object sender, RoutedEventArgs e)
- {
- SelectedItem = SelectBox.SelectedItem;
- Result = true;
- Close();
- }
- private void BtnCancel_Click(object sender, RoutedEventArgs e)
- {
- SelectedItem = null;
- Result = false;
- Close();
- }
- }
- }
|