DynamicGridCodePopupColumn.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Linq;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Data;
  8. using InABox.Clients;
  9. using InABox.Core;
  10. using InABox.WPF;
  11. using Syncfusion.Data;
  12. using Syncfusion.UI.Xaml.Grid;
  13. using Columns = InABox.Core.Columns;
  14. namespace InABox.DynamicGrid;
  15. public class DynamicGridCodePopupColumn<TEntity> : DynamicGridEditorColumn<TEntity,CodePopupEditor,GridTemplateColumn>
  16. where TEntity : BaseObject
  17. {
  18. private IColumns GetLinkColumns(Type type, String prefix)
  19. {
  20. var prefixwithstop = $"{prefix.ToUpper()}.";
  21. var cols = Columns.Create(type);
  22. var props = DatabaseSchema.Properties(typeof(TEntity))
  23. .Where(x => x.Name.ToUpper().StartsWith(prefixwithstop));
  24. foreach (var prop in props)
  25. cols.Add(prop.Name.Remove(0,prefixwithstop.Length));
  26. return cols;
  27. }
  28. protected override void Configure(GridTemplateColumn column, CodePopupEditor editor)
  29. {
  30. var prefix = String.Join(".", Definition.ColumnName.Split('.').Reverse().Skip(1).Reverse());
  31. var colname = String.IsNullOrWhiteSpace(editor.CodeColumn)
  32. ? CoreUtils.PropertyList(editor.Type, p => p.GetEditor() is UniqueCodeEditor)
  33. .FirstOrDefault()?.Name ?? ""
  34. : editor.CodeColumn;
  35. var codecolname = String.IsNullOrWhiteSpace(prefix)
  36. ? colname
  37. : $"{prefix}.{colname}";
  38. ExtraColumns.Add(codecolname);
  39. codecolname = codecolname.Replace('.', '_');
  40. column.CellTemplate = TemplateGenerator.CreateDataTemplate
  41. (
  42. () =>
  43. {
  44. var result = new Label();
  45. result.SetBinding(Label.ContentProperty, new Binding(codecolname));
  46. result.VerticalContentAlignment = VerticalAlignment.Center;
  47. return result;
  48. }
  49. );
  50. column.EditTemplate = TemplateGenerator.CreateDataTemplate
  51. (
  52. () =>
  53. {
  54. var result = new Grid();
  55. result.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Star) });
  56. result.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Auto) });
  57. var textbox = new TextBox();
  58. textbox.CharacterCasing = CharacterCasing.Upper;
  59. textbox.SetBinding(TextBox.TextProperty, new Binding(codecolname));
  60. textbox.SetValue(Grid.ColumnProperty, 0);
  61. textbox.SetValue(Grid.ColumnSpanProperty, 2);
  62. textbox.Padding = new Thickness(2, 0, 0, 0);
  63. textbox.VerticalContentAlignment = VerticalAlignment.Center;
  64. textbox.PreviewKeyDown += (sender, args) => textbox.Tag = true;
  65. textbox.SetValue(FocusManagerHelper.FocusedElementProperty, true);
  66. textbox.Tag = false;
  67. textbox.TextChanged += (sender, args) =>
  68. {
  69. if (Equals(textbox.Tag, false))
  70. textbox.SelectAll();
  71. };
  72. // textbox.GotFocus += (sender, args) =>
  73. // textbox.SelectAll();
  74. textbox.LostFocus += (sender, args) =>
  75. {
  76. if (sender is TextBox { Tag: true } box &&
  77. (sender as FrameworkElement)?.DataContext is DataRowView view)
  78. {
  79. if (String.IsNullOrWhiteSpace(box.Text))
  80. {
  81. var table = new CoreTable();
  82. var columns = GetLinkColumns(editor.Type, prefix);
  83. table.LoadColumns(columns);
  84. var newrow = table.NewRow(true);
  85. UpdateCodePopupColumnValue(newrow,prefix);
  86. }
  87. else
  88. {
  89. var lookup = ClientFactory.CreateClient(editor.Type).Query(
  90. Filter.Create(editor.Type, colname, Operator.BeginsWith,
  91. box.Text),
  92. GetLinkColumns(editor.Type,prefix),
  93. null
  94. );
  95. if (lookup.Rows.Count == 1)
  96. UpdateCodePopupColumnValue(lookup.Rows[0], prefix);
  97. else
  98. PopupCodeList(editor.Type, prefix, codecolname, box.Text, GetLinkColumns(editor.Type,prefix));
  99. }
  100. }
  101. };
  102. result.Children.Add(textbox);
  103. var button = new Button();
  104. button.Content = "..";
  105. button.Width = 25;
  106. button.SetValue(Grid.ColumnProperty, 1);
  107. button.Margin = new Thickness(1);
  108. button.BorderThickness = new Thickness(0.75, 0, 0, 0);
  109. button.Tag = textbox;
  110. button.Click += (sender, args) =>
  111. {
  112. PopupCodeList(editor.Type, prefix, codecolname, (button.Tag as TextBox).Text, GetLinkColumns(editor.Type,prefix));
  113. };
  114. result.Children.Add(button);
  115. return result;
  116. }
  117. );
  118. column.SetCellBoundValue = false;
  119. }
  120. private void PopupCodeList(Type type, string prefix, string codecolname, string value, IColumns columns)
  121. {
  122. var entity = GetEntity?.Invoke() as TEntity;
  123. if (entity != null)
  124. {
  125. var msdtype = typeof(MultiSelectDialog<>).MakeGenericType(type);
  126. var msd = Activator.CreateInstance(
  127. msdtype,
  128. new object[]
  129. {
  130. LookupFactory.DefineFilter<TEntity>(new TEntity[] { entity }, type),
  131. columns,
  132. false
  133. }
  134. ) as IMultiSelectDialog;
  135. if (msd.ShowDialog(codecolname, value, FilterType.StartsWith))
  136. {
  137. var row = msd.Data().Rows.FirstOrDefault();
  138. if (row != null)
  139. UpdateCodePopupColumnValue(row,prefix);
  140. }
  141. }
  142. }
  143. private void UpdateCodePopupColumnValue(CoreRow row, string prefix)
  144. {
  145. var dict = row.ToDictionary();
  146. var updates = new Dictionary<String, object>();
  147. foreach (var key in dict.Keys)
  148. updates[$"{prefix}.{key}"] = dict[key];
  149. UpdateData(updates);
  150. }
  151. public DynamicGridCodePopupColumn(DynamicGridColumn definition) : base(definition)
  152. {
  153. }
  154. }