PopupList.xaml.cs 5.2 KB

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