PopupList.xaml.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Windows;
  5. using System.Windows.Controls;
  6. using System.Windows.Input;
  7. using InABox.Core;
  8. using InABox.Wpf;
  9. namespace InABox.DynamicGrid;
  10. public partial class PopupList : ThemableWindow
  11. {
  12. public delegate void CustomiseGridEvent(IDynamicGrid grid);
  13. public event CustomiseGridEvent? OnCustomiseGrid;
  14. private readonly Dictionary<string, string>? _filters;
  15. private IDynamicGrid _grid;
  16. private readonly string[] _othercolumns = Array.Empty<string>();
  17. private readonly Type _type;
  18. private readonly Type? _gridType;
  19. public PopupList(Type type, Guid id, string[] OtherColumns, Dictionary<string, string>? Filters = null, Type? gridType = null)
  20. {
  21. InitializeComponent();
  22. OtherValues = new Dictionary<string, object?>();
  23. _filters = Filters;
  24. _othercolumns = OtherColumns;
  25. _type = type;
  26. _gridType = gridType;
  27. ID = id;
  28. SourceInitialized += PopupList_SourceInitialized;
  29. }
  30. public Guid ID { get; set; }
  31. public Dictionary<string, object?> OtherValues { get; }
  32. public string AsLookup()
  33. {
  34. var result = new List<object>();
  35. foreach (var key in OtherValues.Keys.ToArray())
  36. {
  37. var comps = key.Split(new[] { "->" }, StringSplitOptions.None);
  38. result.Add(_grid.SelectedRows.First().Get<object>(comps.First()));
  39. }
  40. return string.Join(" / ", result.Where(x => x != null && !string.IsNullOrWhiteSpace(x.ToString())));
  41. }
  42. public event OnDefineFilter? OnDefineFilter;
  43. private void PopupList_SourceInitialized(object? sender, EventArgs e)
  44. {
  45. //_grid = DynamicGridUtils.CreateDynamicGrid(typeof(DynamicDataGrid<>), _type);
  46. _grid = (Activator.CreateInstance(_gridType ?? typeof(DynamicDataGrid<>).MakeGenericType(_type)) as IDynamicGrid)!;
  47. _grid.Margin = new Thickness(5, 5, 5, 0);
  48. ((DependencyObject)_grid).SetValue(Grid.ColumnProperty, 0);
  49. ((DependencyObject)_grid).SetValue(Grid.ColumnSpanProperty, 3);
  50. ((DependencyObject)_grid).SetValue(Grid.RowProperty, 0);
  51. //_grid.AddHiddenColumn("AsLookup");
  52. foreach (var column in _othercolumns)
  53. {
  54. var comps = column.Split(new[] { "->" }, StringSplitOptions.None);
  55. _grid.AddHiddenColumn(comps.First());
  56. OtherValues[column] = null;
  57. }
  58. layoutGrid.Children.Add((UIElement)_grid);
  59. _grid.Reconfigure(options =>
  60. {
  61. options.Clear();
  62. options.SelectColumns = true;
  63. options.FilterRows = true;
  64. });
  65. if(_grid is IDynamicDataGrid dataGrid)
  66. {
  67. dataGrid.ColumnsTag ??= "Popup";
  68. }
  69. _grid.OnDefineFilter += t => OnDefineFilter?.Invoke(t);
  70. _grid.Refresh(true, false);
  71. if (_filters != null)
  72. foreach (var key in _filters.Keys)
  73. {
  74. // TODO: Get this working again
  75. _grid.AddVisualFilter(key, _filters[key]);
  76. }
  77. OnCustomiseGrid?.Invoke(_grid);
  78. _grid.Refresh(false, true);
  79. var screen = WpfScreen.GetScreenFrom(this);
  80. var maxwidth = (int)screen.WorkingArea.Width - 200;
  81. var DesiredWidth = _grid.DesiredWidth();
  82. if (DesiredWidth > maxwidth)
  83. DesiredWidth = maxwidth;
  84. if (DesiredWidth > Width)
  85. Width = DesiredWidth;
  86. var maxheight = (int)screen.WorkingArea.Height - 200;
  87. var DesiredHeight = (int)(Width * 0.6);
  88. if (DesiredHeight > maxheight)
  89. DesiredHeight = maxheight;
  90. if (DesiredHeight > Height)
  91. Height = DesiredHeight;
  92. _grid.SelectedRows = _grid.Data.Rows.Where(r => r.Get<Guid>("ID").Equals(ID)).ToArray();
  93. _grid.OnDoubleClick += Grid_OnDoubleClick;
  94. //Left = screen.DeviceBounds.Left + ((screen.DeviceBounds.Width - Width) / 2.0F);
  95. //Top = screen.DeviceBounds.Top + ((screen.DeviceBounds.Height - Height) / 2.0F);
  96. //WpfScreen.CenterWindowOnScreen(this);
  97. }
  98. private void Grid_OnDoubleClick(object sender, System.ComponentModel.HandledEventArgs args)
  99. {
  100. if (_grid.SelectedRows.Any())
  101. CheckSelectedAndClose();
  102. }
  103. private void OKButton_Click(object sender, RoutedEventArgs e)
  104. {
  105. if (_grid.SelectedRows.Any())
  106. CheckSelectedAndClose();
  107. else
  108. MessageBox.Show("Please select an Item first");
  109. }
  110. private void CheckSelectedAndClose()
  111. {
  112. ID = _grid.SelectedRows.First().Get<Guid>("ID");
  113. //AsLookup = _grid.SelectedRows.First().Get<String>("AsLookup");
  114. foreach (var key in OtherValues.Keys.ToArray())
  115. {
  116. var comps = key.Split(new[] { "->" }, StringSplitOptions.None);
  117. OtherValues[key] = _grid.SelectedRows.First().Get<object>(comps.First());
  118. }
  119. DialogResult = true;
  120. Close();
  121. }
  122. private void CancelButton_Click(object sender, RoutedEventArgs e)
  123. {
  124. DialogResult = false;
  125. Close();
  126. }
  127. }