PopupList.xaml.cs 4.9 KB

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