CurrencyEditorControl.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. using System;
  2. using System.Linq;
  3. using System.Windows;
  4. using System.Windows.Controls;
  5. using System.Windows.Media;
  6. using InABox.Core;
  7. using Syncfusion.Windows.Shared;
  8. namespace InABox.DynamicGrid
  9. {
  10. public class CurrencyEditorControl : DynamicEditorControl<double, CurrencyEditor>
  11. {
  12. static CurrencyEditorControl()
  13. {
  14. //DynamicEditorControlFactory.Register<CurrencyEditorControl, CurrencyEditor>();
  15. }
  16. private CurrencyTextBox? Editor;
  17. private String _currencySymbol = "";
  18. public String CurrencySymbol
  19. {
  20. get => _currencySymbol;
  21. set
  22. {
  23. _currencySymbol = value;
  24. if (Editor != null)
  25. Editor.CurrencySymbol = String.IsNullOrEmpty(value)
  26. ? "$" :
  27. $"{value} ";
  28. }
  29. }
  30. public CurrencyEditorControl()
  31. {
  32. Width = 150;
  33. }
  34. public override void Configure()
  35. {
  36. }
  37. protected override FrameworkElement CreateEditor()
  38. {
  39. var DockPanel = new DockPanel
  40. {
  41. //Orientation = Orientation.Horizontal,
  42. HorizontalAlignment = HorizontalAlignment.Stretch
  43. };
  44. var buttons = CreateButtons(out var bDisableEditor);
  45. foreach(var button in buttons)
  46. {
  47. button.SetValue(DockPanel.DockProperty, Dock.Right);
  48. DockPanel.Children.Add(button);
  49. }
  50. Editor = new CurrencyTextBox
  51. {
  52. HorizontalAlignment = HorizontalAlignment.Stretch,
  53. VerticalAlignment = VerticalAlignment.Stretch,
  54. VerticalContentAlignment = VerticalAlignment.Center,
  55. HorizontalContentAlignment = HorizontalAlignment.Center,
  56. CurrencyDecimalDigits = EditorDefinition.Digits
  57. };
  58. Editor.ValueChanged += (o, e) => { CheckChanged(); };
  59. Editor.SetValue(DockPanel.DockProperty, Dock.Left);
  60. if (bDisableEditor)
  61. {
  62. Editor.Background = new SolidColorBrush(Colors.Silver);
  63. Editor.IsEnabled = false;
  64. }
  65. Editor.CurrencySymbol = String.IsNullOrEmpty(EditorDefinition.CurrencySymbol)
  66. ? "$" :
  67. $"{EditorDefinition.CurrencySymbol} ";
  68. DockPanel.Children.Add(Editor);
  69. return DockPanel;
  70. }
  71. public override int DesiredHeight()
  72. {
  73. return 25;
  74. }
  75. public override int DesiredWidth()
  76. {
  77. var result = 150;
  78. var btnEditor = EditorDefinition as IButtonEditor;
  79. if (btnEditor?.Buttons != null)
  80. foreach (var button in ((IButtonEditor)EditorDefinition).Buttons)
  81. result += button.Width + 5;
  82. return result;
  83. }
  84. protected override double RetrieveValue()
  85. {
  86. return Convert.ToDouble(Editor.Value);
  87. }
  88. protected override void UpdateValue(double value)
  89. {
  90. try
  91. {
  92. Editor.Value = Convert.ToDecimal(value);
  93. }
  94. catch (Exception e)
  95. {
  96. Editor.Value = 0m;
  97. }
  98. }
  99. public override void SetFocus()
  100. {
  101. Editor.Focus();
  102. }
  103. public override void SetColor(Color color)
  104. {
  105. Editor.Background = new SolidColorBrush(color);
  106. }
  107. }
  108. }