DictionaryEdit.xaml.cs 1.6 KB

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