DynamicMultiEditWindow.xaml.cs 10 KB

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