PopupList.xaml.cs 5.4 KB

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