DynamicDashboardGridPresenterEditor.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. using InABox.DynamicGrid;
  2. using InABox.WPF;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Windows;
  9. using System.Windows.Controls;
  10. namespace InABox.Wpf.Dashboard;
  11. public class DynamicDashboardGridPresenterEditor : IDynamicDashboardDataPresenterEditor<DynamicDashboardGridPresenter, DynamicDashboardGridPresenterProperties>
  12. {
  13. private DynamicDashboardDataComponent _dataComponent = null!;
  14. public DynamicDashboardDataComponent DataComponent
  15. {
  16. get => _dataComponent;
  17. set
  18. {
  19. _dataComponent = value;
  20. QueryBox.ItemsSource = DataComponent.Queries.Select(x => x.Key);
  21. if(!DataComponent.TryGetQuery(Properties.Query, out var _query))
  22. {
  23. Properties.Query = DataComponent.Queries.FirstOrDefault()?.Key ?? "";
  24. }
  25. QueryBox.SelectedValue = Properties.Query;
  26. }
  27. }
  28. public DynamicDashboardGridPresenterProperties Properties { get; set; } = null!;
  29. private ContentControl Content = new();
  30. private ComboBox QueryBox = new();
  31. public FrameworkElement? Setup()
  32. {
  33. UpdateData();
  34. var grid = new Grid();
  35. grid.AddRow(GridUnitType.Auto);
  36. grid.AddRow(GridUnitType.Star);
  37. var dock = new DockPanel
  38. {
  39. LastChildFill = false
  40. };
  41. var label = new Label
  42. {
  43. Content = "Query:",
  44. VerticalAlignment = VerticalAlignment.Center,
  45. };
  46. DockPanel.SetDock(label, Dock.Left);
  47. QueryBox.Padding = new(5);
  48. QueryBox.MinWidth = 100;
  49. QueryBox.SelectionChanged += QueryBox_SelectionChanged;
  50. DockPanel.SetDock(QueryBox, Dock.Left);
  51. dock.Children.Add(label);
  52. dock.Children.Add(QueryBox);
  53. Content.Margin = new(0, 5, 0, 0);
  54. grid.AddChild(dock, 0, 0);
  55. grid.AddChild(Content, 1, 0);
  56. Content.Height = 100;
  57. return grid;
  58. }
  59. private void QueryBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  60. {
  61. Properties.Query = QueryBox.SelectedValue as string ?? "";
  62. UpdateData();
  63. }
  64. private void UpdateData()
  65. {
  66. if (!DataComponent.TryGetQuery(Properties.Query, out var query)) return;
  67. var grid = (Activator.CreateInstance(typeof(DynamicItemsListGrid<>).MakeGenericType(query.Type)) as IDynamicGrid)!;
  68. grid.Reconfigure(options =>
  69. {
  70. options.Clear();
  71. options.SelectColumns = true;
  72. });
  73. grid.OnGenerateColumns += (o, e) =>
  74. {
  75. e.Columns.Clear();
  76. if(Properties.Columns is null)
  77. {
  78. e.Columns.AddRange(grid.ExtractColumns(query.Columns));
  79. }
  80. else
  81. {
  82. e.Columns.AddRange(Properties.Columns);
  83. }
  84. };
  85. grid.OnSaveColumns += (o, e) =>
  86. {
  87. Properties.Columns = grid.VisibleColumns;
  88. };
  89. grid.Refresh(true, true);
  90. Content.Content = grid;
  91. }
  92. }