SelectDialog.xaml.cs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. using System.Collections;
  2. using System.Windows;
  3. using HandyControl.Controls;
  4. namespace TeamAAS.Dialogs.Dialogs
  5. {
  6. /// <summary>
  7. /// 选择对话框,用于让用户从列表中选择一项。
  8. /// 通过 DialogHelper.Select 调用。
  9. /// </summary>
  10. public partial class SelectDialog
  11. {
  12. /// <summary>用户选中的项,点击确定后有效;未选择为 null</summary>
  13. public object SelectedItem { get; private set; }
  14. /// <summary>用户是否点击了确定</summary>
  15. public bool Result { get; private set; }
  16. public SelectDialog()
  17. {
  18. InitializeComponent();
  19. UiScaler.Attach(this); // 分辨率/DPI 自适应
  20. }
  21. /// <summary>
  22. /// 配置选择对话框。
  23. /// </summary>
  24. /// <param name="label">提示文本</param>
  25. /// <param name="items">可选项列表</param>
  26. /// <param name="title">窗口标题</param>
  27. /// <param name="defaultItem">默认选中项</param>
  28. public void Setup(string label, IEnumerable items, string title = "选择", object defaultItem = null)
  29. {
  30. Title = title;
  31. LabelText.Text = label;
  32. SelectBox.Items.Clear();
  33. foreach (var item in items)
  34. {
  35. SelectBox.Items.Add(item);
  36. }
  37. if (defaultItem != null)
  38. {
  39. SelectBox.SelectedItem = defaultItem;
  40. }
  41. else if (SelectBox.Items.Count > 0)
  42. {
  43. SelectBox.SelectedIndex = 0;
  44. }
  45. }
  46. private void BtnOK_Click(object sender, RoutedEventArgs e)
  47. {
  48. SelectedItem = SelectBox.SelectedItem;
  49. Result = true;
  50. Close();
  51. }
  52. private void BtnCancel_Click(object sender, RoutedEventArgs e)
  53. {
  54. SelectedItem = null;
  55. Result = false;
  56. Close();
  57. }
  58. }
  59. }