SingleSelector.xaml.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. using System;
  2. using System.Collections;
  3. using System.Windows;
  4. using System.Windows.Controls;
  5. namespace AIPractice.LabellerServer.WPF
  6. {
  7. /// <summary>
  8. /// Interaction logic for SingleSelector.xaml
  9. /// </summary>
  10. public partial class SingleSelector : UserControl
  11. {
  12. private object _selectedItem;
  13. private bool _isEditable;
  14. public event EventHandler Refreshed;
  15. public event EventHandler SelectionChanged;
  16. public bool IsEditable
  17. {
  18. get => _isEditable;
  19. set
  20. {
  21. if (_isEditable != value)
  22. {
  23. _isEditable = value;
  24. if (_isEditable)
  25. {
  26. SingleSelectorButton.Visibility = Visibility.Collapsed;
  27. SingleSelectorTextBox.Visibility = Visibility.Visible;
  28. }
  29. else
  30. {
  31. SingleSelectorButton.Visibility = Visibility.Visible;
  32. SingleSelectorTextBox.Visibility = Visibility.Collapsed;
  33. }
  34. }
  35. }
  36. }
  37. public object SelectedItem
  38. {
  39. get => _selectedItem;
  40. private set
  41. {
  42. if (_selectedItem != value)
  43. {
  44. _selectedItem = value;
  45. if (_selectedItem == null)
  46. {
  47. Text = string.Empty;
  48. }
  49. OnSelectionChanged();
  50. }
  51. }
  52. }
  53. public IEnumerable ItemsSource
  54. {
  55. get => SelectorItems.ItemsSource;
  56. set
  57. {
  58. if (SelectorItems.ItemsSource != value)
  59. {
  60. SelectorItems.ItemsSource = value;
  61. SelectedItem = null;
  62. }
  63. }
  64. }
  65. public string Text
  66. {
  67. get => _isEditable? SingleSelectorTextBox.Text: SingleSelectorButton.Content == null? string.Empty: SingleSelectorButton.Content.ToString();
  68. set
  69. {
  70. SingleSelectorTextBox.Text = value;
  71. SingleSelectorButton.Content = value;
  72. }
  73. }
  74. public SingleSelector()
  75. {
  76. InitializeComponent();
  77. }
  78. private void OnShowItemsClick(object sender, RoutedEventArgs e)
  79. {
  80. PART_Popup.IsOpen = true;
  81. }
  82. private void OnRefreshClick(object sender, RoutedEventArgs e)
  83. {
  84. OnRefreshed();
  85. }
  86. private void OnRefreshed()
  87. {
  88. Refreshed?.Invoke(this, EventArgs.Empty);
  89. }
  90. private void OnItemClick(object sender, RoutedEventArgs e)
  91. {
  92. SelectedItem = ((Button) sender).DataContext;
  93. SingleSelectorButton.Content = SelectedItem.ToString();
  94. SingleSelectorTextBox.Text = SelectedItem.ToString();
  95. PART_Popup.IsOpen = false;
  96. }
  97. private void OnSelectionChanged()
  98. {
  99. SelectionChanged?.Invoke(this, EventArgs.Empty);
  100. }
  101. }
  102. }