DynamicGridPopupColumn.cs 5.5 KB

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