MYOBCompanyFileEditor.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. using InABox.Core;
  2. using InABox.DynamicGrid;
  3. using InABox.WPF;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows;
  10. using System.Windows.Controls;
  11. using System.Windows.Media;
  12. namespace InABox.Poster.MYOB;
  13. public class MYOBCompanyFileEditor : BaseEditor
  14. {
  15. protected override BaseEditor DoClone()
  16. {
  17. return new MYOBCompanyFileEditor();
  18. }
  19. }
  20. public class MYOBCompanyFileEditorControl : DynamicEnclosedEditorControl<MYOBCompanyFile, MYOBCompanyFileEditor>
  21. {
  22. private Grid Grid = null!;
  23. private TextBox TextBox = null!;
  24. private Button Select = null!;
  25. private Button Clear = null!;
  26. private readonly MYOBCompanyFile Value = new();
  27. private static readonly Column<MYOBCompanyFile> IDColumn = new(x => x.ID);
  28. private static readonly Column<MYOBCompanyFile> NameColumn = new(x => x.Name);
  29. public override int DesiredHeight() => 25;
  30. public override int DesiredWidth() => int.MaxValue;
  31. protected override FrameworkElement CreateEditor()
  32. {
  33. Grid = new Grid
  34. {
  35. VerticalAlignment = VerticalAlignment.Stretch,
  36. HorizontalAlignment = HorizontalAlignment.Stretch
  37. };
  38. Grid.AddColumn(GridUnitType.Star);
  39. Grid.AddColumn(70);
  40. Grid.AddColumn(70);
  41. TextBox = new TextBox
  42. {
  43. VerticalAlignment = VerticalAlignment.Stretch,
  44. VerticalContentAlignment = VerticalAlignment.Center,
  45. HorizontalAlignment = HorizontalAlignment.Stretch,
  46. IsEnabled = false
  47. };
  48. Grid.AddChild(TextBox, 0, 0);
  49. Select = new Button
  50. {
  51. VerticalAlignment = VerticalAlignment.Stretch,
  52. VerticalContentAlignment = VerticalAlignment.Center,
  53. HorizontalAlignment = HorizontalAlignment.Stretch,
  54. Content = "Select",
  55. Margin = new Thickness(5, 0, 0, 0),
  56. };
  57. Select.Click += Select_Click;
  58. Grid.AddChild(Select, 0, 1);
  59. Clear = new Button
  60. {
  61. VerticalAlignment = VerticalAlignment.Stretch,
  62. VerticalContentAlignment = VerticalAlignment.Center,
  63. HorizontalAlignment = HorizontalAlignment.Stretch,
  64. Content = "Clear",
  65. Margin = new Thickness(5, 0, 0, 0),
  66. Focusable = false
  67. };
  68. Clear.Click += Clear_Click;
  69. Grid.AddChild(Clear, 0, 2);
  70. return Grid;
  71. }
  72. public override void Configure()
  73. {
  74. }
  75. private void Select_Click(object sender, RoutedEventArgs e)
  76. {
  77. if (e.OriginalSource != Select) return;
  78. var file = MYOBCompanyFileSelectionDialog.SelectCompanyFile();
  79. if(file is not null)
  80. {
  81. Value.ID = file.Id;
  82. Value.Name = file.Name;
  83. TextBox.Text = Value.Name;
  84. CheckChanged();
  85. }
  86. }
  87. private void Clear_Click(object sender, RoutedEventArgs e)
  88. {
  89. Value.ID = Guid.Empty;
  90. Value.Name = "";
  91. TextBox.Text = "";
  92. CheckChanged();
  93. }
  94. protected override object? GetChildValue(string property)
  95. {
  96. if (IDColumn.IsEqualTo(property)) return Value.ID;
  97. if (NameColumn.IsEqualTo(property)) return Value.Name;
  98. return null;
  99. }
  100. protected override void SetChildValue(string property, object? value)
  101. {
  102. if (IDColumn.IsEqualTo(property)) Value.ID = (Guid)(value ?? Guid.Empty);
  103. else if (NameColumn.IsEqualTo(property))
  104. {
  105. Value.Name = (value as string) ?? "";
  106. TextBox.Text = Value.Name;
  107. }
  108. }
  109. protected override IEnumerable<KeyValuePair<string, object?>> GetChildValues()
  110. {
  111. yield return new(IDColumn.Property, Value.ID);
  112. yield return new(NameColumn.Property, Value.Name);
  113. }
  114. public override void SetColor(Color color)
  115. {
  116. TextBox.Background = IsEnabled ? new SolidColorBrush(color) : new SolidColorBrush(Colors.WhiteSmoke);
  117. }
  118. public override void SetFocus()
  119. {
  120. Select.Focus();
  121. }
  122. }