DynamicGridPopupColumn.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Globalization;
  5. using System.Linq;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Data;
  9. using InABox.Core;
  10. using InABox.WPF;
  11. using NPOI.SS.Formula.Functions;
  12. using Syncfusion.UI.Xaml.Grid;
  13. using Syncfusion.UI.Xaml.TreeGrid;
  14. using Columns = InABox.Core.Columns;
  15. namespace InABox.DynamicGrid;
  16. public class PopupConverter : IMultiValueConverter
  17. {
  18. private Dictionary<string, object?> _dictionary = new Dictionary<string, object?>();
  19. private Type _type;
  20. public PopupConverter(IColumns columns, Type type)
  21. {
  22. foreach (var column in columns.ColumnNames())
  23. _dictionary[column] = null;
  24. _type = type;
  25. }
  26. public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
  27. {
  28. if ((values.Length != _dictionary.Keys.Count))
  29. return "";
  30. for (int i = 0; i < values.Length; i++)
  31. _dictionary[_dictionary.Keys.ElementAt(i)] = values[i] == DependencyProperty.UnsetValue ? null : values[i];
  32. return LookupFactory.FormatLookup(_type, _dictionary, Array.Empty<string>());
  33. }
  34. public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
  35. {
  36. throw new NotImplementedException();
  37. }
  38. }
  39. public class DynamicGridPopupColumn<TEntity> : DynamicGridEditorColumn< PopupEditor, GridTemplateColumn, TreeGridTemplateColumn>, IDynamicGridEditorColumn<TEntity>
  40. where TEntity : BaseObject, new()
  41. {
  42. public Func<BaseObject?>? GetEntity { get; set; }
  43. private IColumns GetLinkColumns(Type type, String prefix)
  44. {
  45. var prefixwithstop = $"{prefix.ToUpper()}.";
  46. var cols = Columns.None(type);
  47. var props = DatabaseSchema.Properties(typeof(TEntity))
  48. .Where(x => x.Name.ToUpper().StartsWith(prefixwithstop));
  49. foreach (var prop in props)
  50. cols.Add(prop.Name.Remove(0,prefixwithstop.Length));
  51. return cols;
  52. }
  53. private void PopupList(Type type, string prefix, IColumns columns)
  54. {
  55. var entity = GetEntity?.Invoke() as TEntity;
  56. if (entity != null)
  57. {
  58. var msdtype = typeof(MultiSelectDialog<>).MakeGenericType(type);
  59. var msd = Activator.CreateInstance(
  60. msdtype,
  61. new object?[]
  62. {
  63. LookupFactory.DefineLookupFilter(typeof(TEntity), type, prefix, new TEntity[] { entity }),
  64. columns,
  65. false
  66. }
  67. ) as IMultiSelectDialog;
  68. if (msd.ShowDialog())
  69. {
  70. var row = msd.Data().Rows.FirstOrDefault();
  71. if (row != null)
  72. UpdateColumnValue(row,prefix);
  73. }
  74. }
  75. }
  76. private void UpdateColumnValue(CoreRow row, string prefix)
  77. {
  78. var dict = row.ToDictionary();
  79. var updates = dict.ToDictionary(x => $"{prefix}.{x.Key}", x => x.Value);
  80. this.UpdateData(updates);
  81. }
  82. private Tuple<DataTemplate, DataTemplate> GetTemplates(GridColumnBase column, PopupEditor editor)
  83. {
  84. var prefix = String.Join(".", Definition.ColumnName.Split('.').Reverse().Skip(1).Reverse());
  85. var displaycols = new List<String>();
  86. var lookupcolumns = LookupFactory.DefineLookupColumns(typeof(TEntity), editor.Type, prefix);
  87. foreach (var lookupcolumn in lookupcolumns)
  88. {
  89. var displaycol = String.IsNullOrWhiteSpace(prefix)
  90. ? lookupcolumn.Property
  91. : $"{prefix}.{lookupcolumn.Property}";
  92. ExtraColumns.Add(displaycol);
  93. displaycols.Add(displaycol.Replace('.', '_'));
  94. }
  95. var cell = TemplateGenerator.CreateDataTemplate
  96. (
  97. () =>
  98. {
  99. var result = new Label();
  100. var binding = new MultiBinding();
  101. foreach (var displaycol in displaycols)
  102. binding.Bindings.Add(new Binding(displaycol));
  103. binding.Converter = new PopupConverter(lookupcolumns, editor.Type);
  104. result.SetBinding(Label.ContentProperty, binding);
  105. return result;
  106. }
  107. );
  108. var edit = TemplateGenerator.CreateDataTemplate
  109. (
  110. () =>
  111. {
  112. var result = new DockPanel();
  113. var button = new Button();
  114. button.Content = "..";
  115. button.Width = 25;
  116. button.SetValue(DockPanel.DockProperty, Dock.Right);
  117. button.Tag = column;
  118. button.BorderThickness = new Thickness(0.75, 0, 0, 0);
  119. button.Margin = new Thickness(0);
  120. button.Click += (sender, args) =>
  121. {
  122. PopupList(editor.Type, prefix, GetLinkColumns(editor.Type,prefix));
  123. };
  124. result.Children.Add(button);
  125. var label = new Label();
  126. var binding = new MultiBinding();
  127. foreach (var displaycol in displaycols)
  128. binding.Bindings.Add(new Binding(displaycol));
  129. binding.Converter = new PopupConverter(lookupcolumns, editor.Type);
  130. label.SetBinding(Label.ContentProperty, binding);
  131. label.SetValue(DockPanel.DockProperty, Dock.Left);
  132. result.Children.Add(label);
  133. return result;
  134. }
  135. );
  136. return new(cell, edit);
  137. }
  138. protected override void Configure(TreeGridTemplateColumn column, PopupEditor editor)
  139. {
  140. var (cell, edit) = GetTemplates(column, editor);
  141. column.CellTemplate = cell;
  142. column.EditTemplate = edit;
  143. column.SetCellBoundValue = false;
  144. }
  145. protected override void Configure(GridTemplateColumn column, PopupEditor editor)
  146. {
  147. var (cell, edit) = GetTemplates(column, editor);
  148. column.CellTemplate = cell;
  149. column.EditTemplate = edit;
  150. column.SetCellBoundValue = false;
  151. }
  152. public DynamicGridPopupColumn(DynamicGridColumn definition) : base(definition)
  153. {
  154. }
  155. }