DictionaryRadioEdit.xaml.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. using InABox.Wpf;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Windows;
  5. using System.Windows.Controls;
  6. using FastReport.Data;
  7. using InABox.Core;
  8. namespace InABox.Wpf
  9. {
  10. public class DictionaryRadioItem(DictionaryRadioList parent) : DependencyObject
  11. {
  12. public DictionaryRadioList Parent => parent;
  13. private static readonly DependencyProperty CheckedProperty = DependencyProperty.Register(
  14. nameof(Checked),
  15. typeof(bool),
  16. typeof(DictionaryRadioItem),
  17. new FrameworkPropertyMetadata(
  18. false,
  19. FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
  20. propertyChangedCallback: CheckedChanged
  21. )
  22. );
  23. private static bool _isChanging = false;
  24. private static void CheckedChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  25. {
  26. if (d is not DictionaryRadioItem item || e.NewValue is not bool isChecked || _isChanging)
  27. return;
  28. _isChanging = true;
  29. item.Parent.Selected = isChecked ? item : null;
  30. _isChanging = false;
  31. }
  32. public bool Checked
  33. {
  34. get => (bool)GetValue(CheckedProperty);
  35. set => SetValue(CheckedProperty, value);
  36. }
  37. private static readonly DependencyProperty KeyProperty = DependencyProperty.Register(
  38. nameof(Key),
  39. typeof(object),
  40. typeof(DictionaryRadioItem)
  41. );
  42. public object Key
  43. {
  44. get => GetValue(KeyProperty);
  45. set => SetValue(KeyProperty, value);
  46. }
  47. private static readonly DependencyProperty ValueProperty = DependencyProperty.Register(
  48. nameof(Value),
  49. typeof(string),
  50. typeof(DictionaryRadioItem)
  51. );
  52. public string? Value
  53. {
  54. get => GetValue(ValueProperty) as string;
  55. set => SetValue(ValueProperty, value);
  56. }
  57. }
  58. public class DictionaryRadioList : DependencyObject
  59. {
  60. private void Select(DictionaryRadioItem? selected)
  61. {
  62. var others = Items.Where(x => !Equals(x, selected)).ToArray();
  63. foreach (var item in others)
  64. item.Checked = false;
  65. }
  66. private static readonly DependencyProperty SelectedProperty = DependencyProperty.Register(
  67. nameof(Selected),
  68. typeof(DictionaryRadioItem),
  69. typeof(DictionaryRadioList)
  70. );
  71. public DictionaryRadioItem? Selected
  72. {
  73. get => GetValue(SelectedProperty) as DictionaryRadioItem;
  74. set
  75. {
  76. SetValue(SelectedProperty, value);
  77. Select(Items.FirstOrDefault(x=>Equals(x,value)));
  78. }
  79. }
  80. private static readonly DependencyProperty ItemsProperty = DependencyProperty.Register(
  81. nameof(Items),
  82. typeof(CoreObservableCollection<DictionaryRadioItem>),
  83. typeof(DictionaryRadioList)
  84. );
  85. public CoreObservableCollection<DictionaryRadioItem> Items
  86. {
  87. get => (CoreObservableCollection<DictionaryRadioItem>)GetValue(ItemsProperty);
  88. set => SetValue(ItemsProperty, value);
  89. }
  90. public DictionaryRadioList()
  91. {
  92. Items = new CoreObservableCollection<DictionaryRadioItem>();
  93. }
  94. }
  95. public class DictionaryRadioEditViewModel : DependencyObject
  96. {
  97. private static readonly DependencyProperty TitleProperty = DependencyProperty.Register(
  98. nameof(Title),
  99. typeof(string),
  100. typeof(DictionaryRadioEditViewModel)
  101. );
  102. public string? Title
  103. {
  104. get => GetValue(TitleProperty) as string;
  105. set => SetValue(TitleProperty, value);
  106. }
  107. private static readonly DependencyProperty DescriptionProperty = DependencyProperty.Register(
  108. nameof(Description),
  109. typeof(string),
  110. typeof(DictionaryRadioEditViewModel)
  111. );
  112. public string? Description
  113. {
  114. get => GetValue(DescriptionProperty) as string;
  115. set => SetValue(DescriptionProperty, value);
  116. }
  117. private static readonly DependencyProperty ListProperty = DependencyProperty.Register(
  118. nameof(List),
  119. typeof(DictionaryRadioList),
  120. typeof(DictionaryRadioEditViewModel)
  121. );
  122. public DictionaryRadioList List
  123. {
  124. get => (DictionaryRadioList)GetValue(ListProperty);
  125. set => SetValue(ListProperty, value);
  126. }
  127. }
  128. /// <summary>
  129. /// Interaction logic for PrinterSelection.xaml
  130. /// </summary>
  131. public partial class DictionaryRadioEdit : ThemableWindow
  132. {
  133. public DictionaryRadioEdit(DictionaryRadioList list, string title, string? description )
  134. {
  135. InitializeComponent();
  136. ViewModel.Title = title;
  137. ViewModel.Description = description;
  138. ViewModel.List = list;
  139. }
  140. private void OK_Click(object sender, RoutedEventArgs e)
  141. {
  142. DialogResult = true;
  143. Close();
  144. }
  145. private void Cancel_Click(object sender, RoutedEventArgs e)
  146. {
  147. DialogResult = false;
  148. Close();
  149. }
  150. public static bool Execute<T>(Dictionary<T, string> values, string title, string? description, ref T? result) where T : notnull
  151. {
  152. var list = new DictionaryRadioList();
  153. foreach (var key in values.Keys)
  154. list.Items.Add(new DictionaryRadioItem(list) { Key = key, Value = values[key], Checked = Equals(result,key)});
  155. var edit = new DictionaryRadioEdit(
  156. list,
  157. title,
  158. description);
  159. if (edit.ShowDialog() == true)
  160. {
  161. result = list.Selected != null ? (T)list.Selected.Key : default;
  162. return result is not null;
  163. }
  164. return false;
  165. }
  166. }
  167. }