DynamicMultiEditWindow.xaml.cs 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Linq;
  5. using System.Windows;
  6. using InABox.Core;
  7. using InABox.Wpf;
  8. using Syncfusion.UI.Xaml.Grid;
  9. namespace InABox.DynamicGrid
  10. {
  11. /// <summary>
  12. /// Interaction logic for DynamicMultiEditWindow.xaml
  13. /// </summary>
  14. public partial class DynamicMultiEditWindow : ThemableWindow, IDynamicEditorForm
  15. {
  16. public DynamicMultiEditWindow()
  17. {
  18. InitializeComponent();
  19. }
  20. protected virtual void EndEdit(int row, int col, object value)
  21. {
  22. }
  23. protected virtual void DeleteRow(int row)
  24. {
  25. }
  26. private void Editor_CurrentCellEndEdit(object sender, CurrentCellEndEditEventArgs e)
  27. {
  28. //if (!(Editor.CurrentCellInfo.Column is GridComboBoxColumn))
  29. // EndEdit(e.RowColumnIndex.RowIndex-1, e.RowColumnIndex.ColumnIndex, null);
  30. }
  31. private void OK_Click(object sender, RoutedEventArgs e)
  32. {
  33. DialogResult = true;
  34. Close();
  35. }
  36. private void Cancel_Click(object sender, RoutedEventArgs e)
  37. {
  38. DialogResult = false;
  39. Close();
  40. }
  41. private void CurrentCellActivated(object sender, CurrentCellActivatedEventArgs args)
  42. {
  43. //Skip the selection when it is moved to next row on adding new row.       
  44. //bool needToMove = (Editor.IsAddNewIndex(args.PreviousRowColumnIndex.RowIndex)
  45. // //&& !Editor.IsAddNewIndex(args.CurrentRowColumnIndex.RowIndex)
  46. // && (args.ActivationTrigger == ActivationTrigger.Keyboard)
  47. // && (Keyboard.IsKeyDown(Key.Enter) || Keyboard.IsKeyDown(Key.Tab)));
  48. //if (needToMove)
  49. //{
  50. // Dispatcher.BeginInvoke(
  51. // new Action(
  52. // () =>
  53. // {
  54. // Editor.MoveCurrentCell
  55. // (
  56. // new Syncfusion.UI.Xaml.ScrollAxis.RowColumnIndex
  57. // (
  58. // args.PreviousRowColumnIndex.RowIndex-1,
  59. // args.CurrentRowColumnIndex.ColumnIndex
  60. // )
  61. // );
  62. // }
  63. // )
  64. // );
  65. //}
  66. }
  67. private void Delete_Click(object sender, RoutedEventArgs e)
  68. {
  69. DeleteRow(Editor.CurrentCellInfo.RowIndex);
  70. }
  71. private void Editor_CurrentCellValueChanged(object sender, CurrentCellValueChangedEventArgs e)
  72. {
  73. //EndEdit(e.RowColumnIndex.RowIndex - 1, e.RowColumnIndex.ColumnIndex);
  74. }
  75. private void Editor_CurrentCellDropDownSelectionChanged(object sender, CurrentCellDropDownSelectionChangedEventArgs e)
  76. {
  77. //EndEdit(e.RowColumnIndex.RowIndex - 1, e.RowColumnIndex.ColumnIndex, (e.SelectedItem as DataRowView)["ID"]);
  78. }
  79. }
  80. public class DynamicEditWindow<T> : DynamicMultiEditWindow where T : BaseObject, new()
  81. {
  82. private CoreTable _data;
  83. private readonly DataTable table = new();
  84. private readonly Dictionary<int, T> updates = new();
  85. public T[] Updates => updates.Values.ToArray();
  86. public event OnCustomiseColumns? OnCustomiseColumns;
  87. public event OnGetEditor? OnGetEditor;
  88. public event OnCreateItem<T>? OnCreateItem;
  89. public event OnGetEditorSequence? OnGetSequence;
  90. private decimal GetSequence(DynamicGridColumn column)
  91. {
  92. if (OnGetSequence != null)
  93. return OnGetSequence.Invoke(column);
  94. return 999M;
  95. }
  96. public void Load(CoreTable data)
  97. {
  98. _data = data;
  99. var dgcs = OnCustomiseColumns?.Invoke(this, null).OrderBy(x => GetSequence(x)).ToArray();
  100. foreach (var dgc in dgcs)
  101. {
  102. var col = data.Columns.FirstOrDefault(x => string.Equals(x.ColumnName, dgc.ColumnName));
  103. if (col != null)
  104. table.Columns.Add(col.ColumnName, col.DataType);
  105. }
  106. foreach (var row in data.Rows)
  107. {
  108. var newrow = table.NewRow();
  109. foreach (var col in dgcs)
  110. if (table.Columns.Contains(col.ColumnName))
  111. newrow[col.ColumnName] = row[col.ColumnName];
  112. table.Rows.Add(newrow);
  113. }
  114. foreach (var dgc in dgcs)
  115. try
  116. {
  117. var editor = OnGetEditor?.Invoke(dgc);
  118. if (editor != null && editor.Editable != Editable.Hidden)
  119. {
  120. GridColumn col = null;
  121. if (editor is TextBoxEditor)
  122. {
  123. col = new GridTextColumn { ColumnSizer = GridLengthUnitType.Star };
  124. }
  125. else if (editor is CodeEditor)
  126. {
  127. col = new GridMaskColumn { Mask = ">a" };
  128. }
  129. else if (editor is CheckBoxEditor)
  130. {
  131. col = new GridCheckBoxColumn();
  132. }
  133. else if (editor is DateTimeEditor)
  134. {
  135. col = new GridDateTimeColumn();
  136. }
  137. else if (editor is DateEditor)
  138. {
  139. col = new GridDateTimeColumn();
  140. }
  141. else if (editor is TimeOfDayEditor)
  142. {
  143. col = new GridTimeSpanColumn();
  144. }
  145. else if (editor is DurationEditor)
  146. {
  147. col = new GridTimeSpanColumn();
  148. }
  149. else if (editor is PINEditor)
  150. {
  151. col = new GridMaskColumn { Mask = "9999" };
  152. }
  153. // //else if (editor is CheckListEditor)
  154. // // element = new CheckListBoxEditorControl();
  155. // //else if (editor is MemoEditor)
  156. // // element = new MemoEditorControl();
  157. else if (editor is LookupEditor || editor is EnumLookupEditor || editor is ComboLookupEditor)
  158. {
  159. var values = (editor as ILookupEditor)?.Values(dgc.ColumnName)?.ToDataTable()?.AsDataView();
  160. col = new GridComboBoxColumn { DisplayMemberPath = "Display", SelectedValuePath = "ID", ItemsSource = values };
  161. }
  162. // //else if (editor is MiscellaneousDocumentEditor)
  163. // // element = new DocumentEditorControl();
  164. // //else if (editor is ImageDocumentEditor)
  165. // // element = new DocumentEditorControl();
  166. // //else if (editor is PDFDocumentEditor)
  167. // // element = new DocumentEditorControl();
  168. else if (editor is CurrencyEditor)
  169. {
  170. col = new GridCurrencyColumn();
  171. }
  172. else if (editor is DoubleEditor)
  173. {
  174. col = new GridNumericColumn { ParsingMode = ParseMode.Double };
  175. }
  176. else if (editor is IntegerEditor)
  177. {
  178. col = new GridNumericColumn { ParsingMode = ParseMode.Int };
  179. }
  180. //else if (editor is InABox.Core.ScriptEditor)
  181. // element = new ScriptEditorControl();
  182. //else if (editor is PopupEditor)
  183. // element = new PopupEditorControl();
  184. //else if (editor is TimestampEditor)
  185. // element = new TimestampEditorControl();
  186. //else if (editor is ColorEditor)
  187. // element = new ColorEditorControl();
  188. if (col != null)
  189. {
  190. col.MappingName = dgc.ColumnName;
  191. col.UseBindingValue = true;
  192. Editor.Columns.Add(col);
  193. }
  194. }
  195. }
  196. catch (Exception e)
  197. {
  198. Logger.Send(LogType.Error, "", string.Format("*** Unknown Error: {0}\n{1}", e.Message, e.StackTrace));
  199. }
  200. }
  201. protected override void EndEdit(int row, int col, object value)
  202. {
  203. if (!updates.ContainsKey(row))
  204. {
  205. if (row < _data.Rows.Count)
  206. updates[row] = _data.Rows[row].ToObject<T>();
  207. else
  208. updates[row] = OnCreateItem?.Invoke();
  209. }
  210. object cur = updates[row];
  211. var property = Editor.Columns[col].MappingName;
  212. var currow = Editor.View.IsAddingNew
  213. ? (Editor.View.CurrentAddItem as DataRowView).Row
  214. : table.Rows[row];
  215. var val = value == null ? currow[property] : value;
  216. currow[property] = val;
  217. CoreUtils.SetPropertyValue(cur, property, val);
  218. }
  219. protected override void OnActivated(EventArgs e)
  220. {
  221. base.OnActivated(e);
  222. Editor.ItemsSource = table;
  223. }
  224. protected override void DeleteRow(int row)
  225. {
  226. if (Editor.IsAddNewIndex(row))
  227. Editor.View.CancelNew();
  228. else if (row < table.Rows.Count)
  229. table.Rows.RemoveAt(row);
  230. }
  231. }
  232. }