PopupList.xaml.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. options.ReadOnly = true;
  65. });
  66. if(_grid is IDynamicDataGrid dataGrid)
  67. {
  68. dataGrid.ColumnsTag ??= "Popup";
  69. }
  70. _grid.OnDefineFilter += t => OnDefineFilter?.Invoke(t);
  71. _grid.Refresh(true, false);
  72. if (_filters != null)
  73. foreach (var key in _filters.Keys)
  74. {
  75. // TODO: Get this working again
  76. _grid.AddVisualFilter(key, _filters[key]);
  77. }
  78. OnCustomiseGrid?.Invoke(_grid);
  79. _grid.Refresh(false, true);
  80. var screen = WpfScreen.GetScreenFrom(this);
  81. var maxwidth = (int)screen.WorkingArea.Width - 200;
  82. var DesiredWidth = _grid.DesiredWidth();
  83. if (DesiredWidth > maxwidth)
  84. DesiredWidth = maxwidth;
  85. if (DesiredWidth > Width)
  86. Width = DesiredWidth;
  87. var maxheight = (int)screen.WorkingArea.Height - 200;
  88. var DesiredHeight = (int)(Width * 0.6);
  89. if (DesiredHeight > maxheight)
  90. DesiredHeight = maxheight;
  91. if (DesiredHeight > Height)
  92. Height = DesiredHeight;
  93. _grid.SelectedRows = _grid.Data.Rows.Where(r => r.Get<Guid>("ID").Equals(ID)).ToArray();
  94. _grid.OnDoubleClick += Grid_OnDoubleClick;
  95. //Left = screen.DeviceBounds.Left + ((screen.DeviceBounds.Width - Width) / 2.0F);
  96. //Top = screen.DeviceBounds.Top + ((screen.DeviceBounds.Height - Height) / 2.0F);
  97. //WpfScreen.CenterWindowOnScreen(this);
  98. }
  99. private void Grid_OnDoubleClick(object sender, System.ComponentModel.HandledEventArgs args)
  100. {
  101. if (_grid.SelectedRows.Any())
  102. CheckSelectedAndClose();
  103. }
  104. private void OKButton_Click(object sender, RoutedEventArgs e)
  105. {
  106. if (_grid.SelectedRows.Any())
  107. CheckSelectedAndClose();
  108. else
  109. MessageBox.Show("Please select an Item first");
  110. }
  111. private void CheckSelectedAndClose()
  112. {
  113. ID = _grid.SelectedRows.First().Get<Guid>("ID");
  114. //AsLookup = _grid.SelectedRows.First().Get<String>("AsLookup");
  115. foreach (var key in OtherValues.Keys.ToArray())
  116. {
  117. var comps = key.Split(new[] { "->" }, StringSplitOptions.None);
  118. OtherValues[key] = _grid.SelectedRows.First().Get<object>(comps.First());
  119. }
  120. DialogResult = true;
  121. Close();
  122. }
  123. private void CancelButton_Click(object sender, RoutedEventArgs e)
  124. {
  125. DialogResult = false;
  126. Close();
  127. }
  128. }