CustomToolBarPanel.cs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. using System;
  2. using System.ComponentModel;
  3. using System.Windows;
  4. using System.Windows.Controls;
  5. using System.Windows.Forms;
  6. namespace CustomControls
  7. {
  8. [DesignTimeVisible(false)]
  9. public class CustomToolBarPanel : System.Windows.Controls.Primitives.ToolBarPanel
  10. {
  11. protected override Size MeasureOverride(Size constraint)
  12. {
  13. if (TemplatedParent is ToolBar tb && tb.Tag is StatusStrip)
  14. {
  15. double width = 0;
  16. foreach (FrameworkElement item in InternalChildren)
  17. {
  18. item.Measure(constraint);
  19. if (!(item.Tag is ToolStripStatusLabel lbl && lbl.Spring))
  20. width += item.DesiredSize.Width;
  21. }
  22. double springWidth = Math.Max(constraint.Width - width, 0);
  23. foreach (FrameworkElement item in InternalChildren)
  24. {
  25. if (item.Tag is ToolStripStatusLabel lbl && lbl.Spring)
  26. {
  27. item.Width = springWidth;
  28. item.Height = constraint.Height;
  29. item.Measure(new Size(item.Width, item.Height));
  30. }
  31. }
  32. return constraint;
  33. }
  34. return base.MeasureOverride(constraint);
  35. }
  36. }
  37. }