PrinterSelection.xaml.cs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. using InABox.Wpf;
  2. using System.Drawing.Printing;
  3. using System.Windows;
  4. using System.Windows.Controls;
  5. namespace InABox.WPF
  6. {
  7. /// <summary>
  8. /// Interaction logic for PrinterSelection.xaml
  9. /// </summary>
  10. public partial class PrinterSelection : ThemableWindow
  11. {
  12. private readonly List<string> printers = new() { "" };
  13. public PrinterSelection(string value)
  14. {
  15. InitializeComponent();
  16. foreach (string printer in PrinterSettings.InstalledPrinters)
  17. printers.Add(printer);
  18. Combo.ItemsSource = printers;
  19. Value = Combo.Items.Contains(value) ? value : "";
  20. }
  21. public string Value
  22. {
  23. get => Combo.SelectedItem.ToString();
  24. set => Combo.SelectedItem = value;
  25. }
  26. private void OK_Click(object sender, RoutedEventArgs e)
  27. {
  28. DialogResult = true;
  29. Close();
  30. }
  31. private void Cancel_Click(object sender, RoutedEventArgs e)
  32. {
  33. DialogResult = false;
  34. Close();
  35. }
  36. public static bool Execute(ref string value)
  37. {
  38. var edit = new PrinterSelection(value);
  39. if (edit.ShowDialog() == true)
  40. {
  41. value = edit.Value;
  42. return true;
  43. }
  44. return false;
  45. }
  46. private void Combo_SelectionChanged(object sender, SelectionChangedEventArgs e)
  47. {
  48. OK.IsEnabled = !string.IsNullOrWhiteSpace(Combo.SelectedValue.ToString());
  49. }
  50. }
  51. }