ProductChargeEditor.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. using Comal.Classes;
  2. using InABox.DynamicGrid;
  3. using Syncfusion.Windows.Shared;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows;
  11. using System.Windows.Controls;
  12. using System.Windows.Media;
  13. using Syncfusion.Windows.Tools.Controls;
  14. namespace PRS.Shared
  15. {
  16. public class ProductPriceEditorControl : DynamicEnclosedEditorControl<ProductCharge, ProductChargeEditor>
  17. {
  18. private DockPanel _dockpanel;
  19. private ComboBox _chargeable;
  20. private ComboBox _pricetype;
  21. private Border _priceborder;
  22. private Label _fixedpricelabel;
  23. private DoubleTextBox _pricevalue;
  24. private Label _markuplabel;
  25. private ProductCharge _productCharge = new();
  26. public override void Configure()
  27. {
  28. }
  29. private readonly Dictionary<ProductPriceType, Tuple<String, String>> _pricetypelabel =
  30. new Dictionary<ProductPriceType, Tuple<String, String>>()
  31. {
  32. { ProductPriceType.FixedPrice, new Tuple<String, String>("$", "") },
  33. { ProductPriceType.CostPlus, new Tuple<String, String>("", "%") },
  34. };
  35. protected override FrameworkElement CreateEditor()
  36. {
  37. _chargeable = new ComboBox()
  38. {
  39. ItemsSource = new String[] { "Unchargeable", "Chargeable" },
  40. VerticalContentAlignment = VerticalAlignment.Center,
  41. HorizontalContentAlignment = HorizontalAlignment.Center,
  42. SelectedIndex = _productCharge.Chargeable ? 1 : 0,
  43. Width = 150.0F
  44. };
  45. _chargeable.SelectionChanged += _chargeable_Changed;
  46. _pricetype = new ComboBox
  47. {
  48. ItemsSource = Enum.GetValues<ProductPriceType>(),
  49. VerticalContentAlignment = VerticalAlignment.Center,
  50. HorizontalContentAlignment = HorizontalAlignment.Center,
  51. SelectedValue = _productCharge.PriceType,
  52. Margin = new Thickness(5,0,0,0),
  53. Width = 100.0F,
  54. Visibility = _productCharge.Chargeable ? Visibility.Visible : Visibility.Collapsed
  55. };
  56. _pricetype.SelectionChanged += _pricetype_SelectionChanged;
  57. _fixedpricelabel = new Label()
  58. {
  59. Content = _pricetypelabel[_productCharge.PriceType].Item1,
  60. Margin = new Thickness(5,0,0,0),
  61. Padding = new Thickness(0),
  62. HorizontalContentAlignment = HorizontalAlignment.Center,
  63. VerticalContentAlignment = VerticalAlignment.Center
  64. };
  65. _pricevalue = new DoubleTextBox
  66. {
  67. Padding = new Thickness(0),
  68. Margin= new Thickness(0),
  69. VerticalContentAlignment = VerticalAlignment.Center,
  70. HorizontalContentAlignment = HorizontalAlignment.Center,
  71. VerticalAlignment = VerticalAlignment.Stretch,
  72. BorderThickness = new Thickness(0.0F),
  73. };
  74. _pricevalue.ValueChanged += _pricevalue_Changed;
  75. _markuplabel = new Label()
  76. {
  77. Content = _pricetypelabel[_productCharge.PriceType].Item2,
  78. Margin= new Thickness(0,0,5,0),
  79. Padding = new Thickness(0),
  80. HorizontalContentAlignment = HorizontalAlignment.Center,
  81. VerticalContentAlignment = VerticalAlignment.Center
  82. };
  83. Grid grid = new Grid()
  84. {
  85. ColumnDefinitions =
  86. {
  87. new ColumnDefinition() { Width = new GridLength(20.0F, GridUnitType.Pixel) },
  88. new ColumnDefinition() { Width = new GridLength(60.0F, GridUnitType.Pixel) },
  89. new ColumnDefinition() { Width = new GridLength(20.0F, GridUnitType.Pixel) },
  90. }
  91. };
  92. _fixedpricelabel.SetValue(Grid.ColumnProperty, 0);
  93. grid.Children.Add(_fixedpricelabel);
  94. _pricevalue.SetValue(Grid.ColumnProperty, 1);
  95. grid.Children.Add(_pricevalue);
  96. _markuplabel.SetValue(Grid.ColumnProperty, 2);
  97. grid.Children.Add(_markuplabel);
  98. _priceborder = new Border()
  99. {
  100. BorderBrush = new SolidColorBrush(Colors.Gray),
  101. BorderThickness = new Thickness(0.75),
  102. Margin = new Thickness(5,0,0,0),
  103. Padding = new Thickness(0),
  104. Visibility = _productCharge.Chargeable ? Visibility.Visible : Visibility.Collapsed,
  105. Child = grid
  106. };
  107. _dockpanel = new DockPanel() { LastChildFill = false };
  108. _chargeable.SetValue(DockPanel.DockProperty, Dock.Left);
  109. _dockpanel.Children.Add(_chargeable);
  110. _pricetype.SetValue(DockPanel.DockProperty, Dock.Left);
  111. _dockpanel.Children.Add(_pricetype);
  112. _priceborder.SetValue(DockPanel.DockProperty, Dock.Left);
  113. _dockpanel.Children.Add(_priceborder);
  114. return _dockpanel;
  115. }
  116. public override void SetValue(string property, object? value)
  117. {
  118. if (!property.StartsWith($"{ColumnName}.")) return;
  119. property = property[(ColumnName.Length + 1)..];
  120. if (property == nameof(_productCharge.Chargeable) && value is bool b)
  121. {
  122. _productCharge.Chargeable = b;
  123. _chargeable.SelectedIndex = b ? 1 : 0;
  124. }
  125. else if (property == nameof(_productCharge.Markup) && value is double markup)
  126. {
  127. _productCharge.Markup = markup;
  128. if (_productCharge.PriceType == ProductPriceType.CostPlus)
  129. {
  130. _pricevalue.Value = markup;
  131. }
  132. }
  133. else if (property == nameof(_productCharge.Price) && value is double price)
  134. {
  135. _productCharge.Price = price;
  136. if(_productCharge.PriceType == ProductPriceType.FixedPrice)
  137. {
  138. _pricevalue.Value = price;
  139. }
  140. }
  141. else if (property == nameof(_productCharge.PriceType) && value is ProductPriceType priceType)
  142. {
  143. _productCharge.PriceType = priceType;
  144. _pricetype.SelectedValue = priceType;
  145. }
  146. }
  147. public override object? GetValue(string property)
  148. {
  149. if (!property.StartsWith($"{ColumnName}.")) return null;
  150. property = property[(ColumnName.Length + 1)..];
  151. if (property == nameof(_productCharge.Chargeable)) return _productCharge.Chargeable;
  152. if (property == nameof(_productCharge.Markup)) return _productCharge.Markup;
  153. if (property == nameof(_productCharge.Price)) return _productCharge.Price;
  154. if (property == nameof(_productCharge.PriceType)) return _productCharge.PriceType;
  155. return null;
  156. }
  157. public override Dictionary<string, object?> GetValues()
  158. {
  159. return new Dictionary<string, object?>
  160. {
  161. { $"{ColumnName}.{nameof(_productCharge.Chargeable)}", _productCharge.Chargeable },
  162. { $"{ColumnName}.{nameof(_productCharge.Markup)}", _productCharge.Markup },
  163. { $"{ColumnName}.{nameof(_productCharge.Price)}", _productCharge.Price },
  164. { $"{ColumnName}.{nameof(_productCharge.PriceType)}", _productCharge.PriceType }
  165. };
  166. }
  167. private void _chargeable_Changed(object sender, SelectionChangedEventArgs e)
  168. {
  169. _productCharge.Chargeable = _chargeable.SelectedIndex == 1;
  170. _pricetype.Visibility = _productCharge.Chargeable ? Visibility.Visible : Visibility.Collapsed;
  171. _priceborder.Visibility = _productCharge.Chargeable ? Visibility.Visible : Visibility.Collapsed;
  172. }
  173. private void _pricetype_SelectionChanged(object sender, SelectionChangedEventArgs e)
  174. {
  175. if(_pricetype.SelectedValue is ProductPriceType priceType)
  176. {
  177. _productCharge.PriceType = priceType;
  178. _pricevalue.Value = priceType switch
  179. {
  180. ProductPriceType.CostPlus => _productCharge.Markup,
  181. _ => _productCharge.Price,
  182. };
  183. _fixedpricelabel.Content = _pricetypelabel[priceType].Item1;
  184. _markuplabel.Content = _pricetypelabel[priceType].Item2;
  185. }
  186. CheckChanged();
  187. }
  188. private void _pricevalue_Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)
  189. {
  190. var price = _pricevalue.Value ?? 0.0;
  191. switch (_productCharge.PriceType)
  192. {
  193. case ProductPriceType.FixedPrice:
  194. _productCharge.Price = price;
  195. break;
  196. case ProductPriceType.CostPlus:
  197. _productCharge.Markup = price;
  198. break;
  199. }
  200. CheckChanged();
  201. }
  202. public override int DesiredHeight()
  203. {
  204. return 25;
  205. }
  206. public override int DesiredWidth()
  207. {
  208. return int.MaxValue;
  209. }
  210. public override void SetColor(System.Windows.Media.Color color)
  211. {
  212. var brush = new SolidColorBrush(color);
  213. // _chargeable.Background = brush;
  214. // _pricetype.Background = brush;
  215. _pricevalue.Background = brush;
  216. _fixedpricelabel.Background = brush;
  217. _markuplabel.Background = brush;
  218. _priceborder.Background = brush;
  219. }
  220. public override void SetFocus()
  221. {
  222. _pricevalue.Focus();
  223. }
  224. }
  225. }