PopupList.xaml.cs 5.1 KB

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