123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- using System;
- using System.Collections;
- using System.Windows;
- using System.Windows.Controls;
- namespace AIPractice.LabellerServer.WPF
- {
- /// <summary>
- /// Interaction logic for SingleSelector.xaml
- /// </summary>
- public partial class SingleSelector : UserControl
- {
- private object _selectedItem;
- private bool _isEditable;
- public event EventHandler Refreshed;
- public event EventHandler SelectionChanged;
- public bool IsEditable
- {
- get => _isEditable;
- set
- {
- if (_isEditable != value)
- {
- _isEditable = value;
- if (_isEditable)
- {
- SingleSelectorButton.Visibility = Visibility.Collapsed;
- SingleSelectorTextBox.Visibility = Visibility.Visible;
- }
- else
- {
- SingleSelectorButton.Visibility = Visibility.Visible;
- SingleSelectorTextBox.Visibility = Visibility.Collapsed;
- }
- }
- }
- }
- public object SelectedItem
- {
- get => _selectedItem;
- private set
- {
- if (_selectedItem != value)
- {
- _selectedItem = value;
- if (_selectedItem == null)
- {
- Text = string.Empty;
- }
- OnSelectionChanged();
- }
- }
- }
- public IEnumerable ItemsSource
- {
- get => SelectorItems.ItemsSource;
- set
- {
- if (SelectorItems.ItemsSource != value)
- {
- SelectorItems.ItemsSource = value;
- SelectedItem = null;
- }
- }
- }
- public string Text
- {
- get => _isEditable? SingleSelectorTextBox.Text: SingleSelectorButton.Content == null? string.Empty: SingleSelectorButton.Content.ToString();
- set
- {
- SingleSelectorTextBox.Text = value;
- SingleSelectorButton.Content = value;
- }
- }
- public SingleSelector()
- {
- InitializeComponent();
- }
- private void OnShowItemsClick(object sender, RoutedEventArgs e)
- {
- PART_Popup.IsOpen = true;
- }
- private void OnRefreshClick(object sender, RoutedEventArgs e)
- {
- OnRefreshed();
- }
- private void OnRefreshed()
- {
- Refreshed?.Invoke(this, EventArgs.Empty);
- }
- private void OnItemClick(object sender, RoutedEventArgs e)
- {
- SelectedItem = ((Button) sender).DataContext;
- SingleSelectorButton.Content = SelectedItem.ToString();
- SingleSelectorTextBox.Text = SelectedItem.ToString();
- PART_Popup.IsOpen = false;
- }
- private void OnSelectionChanged()
- {
- SelectionChanged?.Invoke(this, EventArgs.Empty);
- }
- }
- }
|