DictionaryComboEdit.xaml.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. using InABox.Wpf;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Windows;
  5. using System.Windows.Controls;
  6. namespace InABox.WPF
  7. {
  8. /// <summary>
  9. /// Interaction logic for PrinterSelection.xaml
  10. /// </summary>
  11. public partial class DictionaryComboEdit : ThemableWindow
  12. {
  13. public string Description
  14. {
  15. get => Label.Content as string ?? "Select Value";
  16. set => Label.Content = value;
  17. }
  18. public DictionaryComboEdit(string[] values, ref int index)
  19. {
  20. InitializeComponent();
  21. Combo.Items.Clear();
  22. Combo.Items.Add("");
  23. foreach (var val in values)
  24. Combo.Items.Add(val);
  25. Index = index;
  26. }
  27. public int Index
  28. {
  29. get => Combo.SelectedIndex - 1;
  30. set => Combo.SelectedIndex = value + 1;
  31. }
  32. private void OK_Click(object sender, RoutedEventArgs e)
  33. {
  34. DialogResult = true;
  35. Close();
  36. }
  37. private void Cancel_Click(object sender, RoutedEventArgs e)
  38. {
  39. DialogResult = false;
  40. Close();
  41. }
  42. public static bool Execute<T>(Dictionary<T, string> values, ref T value, string? title = null, string? description = null) where T : notnull
  43. {
  44. var index = values.Keys.ToList().IndexOf(value);
  45. var edit = new DictionaryComboEdit(values.Values.ToArray(), ref index);
  46. edit.Title = title ?? "Value Selection";
  47. edit.Description = description ?? "Select Value";
  48. if (edit.ShowDialog() == true)
  49. {
  50. value = edit.Index > -1 ? values.Keys.ToList()[edit.Index] : default;
  51. return true;
  52. }
  53. return false;
  54. }
  55. private void Combo_SelectionChanged(object sender, SelectionChangedEventArgs e)
  56. {
  57. OK.IsEnabled = Combo.SelectedIndex > 0;
  58. }
  59. }
  60. }