DefaultDynamicEditorGridLayout.cs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. namespace InABox.DynamicGrid
  9. {
  10. public class DefaultDynamicEditorGridLayout : DynamicEditorGridLayout
  11. {
  12. public override bool TabStripVisible
  13. {
  14. get { return Details.TabStripVisible; }
  15. set { Details.TabStripVisible = value; }
  16. }
  17. private DynamicTabControl Details;
  18. private double PageWidth = 0;
  19. private double PageHeight = 0;
  20. public override double TotalWidth => PageWidth;
  21. public override double TotalHeight => PageHeight;
  22. public DefaultDynamicEditorGridLayout()
  23. {
  24. Details = new DynamicTabControl();
  25. Details.VerticalAlignment = VerticalAlignment.Stretch;
  26. Details.HorizontalAlignment = HorizontalAlignment.Stretch;
  27. Details.Name = "Details";
  28. Details.SelectionChanged += Details_SelectionChanged;
  29. Content = Details;
  30. }
  31. public override void LoadPages(IEnumerable<IDynamicEditorPage> pages)
  32. {
  33. Details.Items.Clear();
  34. foreach (var page in pages.OrderBy(x => x.PageType).ThenBy(x => x.Order()).ThenBy(x => x.Caption()))
  35. {
  36. var tab = new DynamicTabItem();
  37. tab.Header = page.Caption();
  38. tab.Content = page;
  39. Details.Items.Add(tab);
  40. var minSize = page.MinimumSize();
  41. PageWidth = Math.Max(PageWidth, minSize.Width);
  42. PageHeight = Math.Max(PageHeight, minSize.Height);
  43. }
  44. Details.SelectedItem = Details.Items.Count > 0 ? Details.Items[0] : null;
  45. }
  46. private bool bChanging;
  47. private void Details_SelectionChanged(object sender, SelectionChangedEventArgs e)
  48. {
  49. if (bChanging || Details?.SelectedItem == null || e.OriginalSource != Details)
  50. return;
  51. bChanging = true;
  52. try
  53. {
  54. var tab = Details.SelectedItem as DynamicTabItem;
  55. if (tab is not null && tab.Content is IDynamicEditorPage page)
  56. {
  57. SelectPage(page);
  58. }
  59. }
  60. finally
  61. {
  62. bChanging = false;
  63. }
  64. }
  65. }
  66. }