using System; using System.ComponentModel; using System.Windows; using System.Windows.Controls; using System.Windows.Forms; namespace CustomControls { [DesignTimeVisible(false)] public class CustomToolBarPanel : System.Windows.Controls.Primitives.ToolBarPanel { protected override Size MeasureOverride(Size constraint) { if (TemplatedParent is ToolBar tb && tb.Tag is StatusStrip) { double width = 0; foreach (FrameworkElement item in InternalChildren) { item.Measure(constraint); if (!(item.Tag is ToolStripStatusLabel lbl && lbl.Spring)) width += item.DesiredSize.Width; } double springWidth = Math.Max(constraint.Width - width, 0); foreach (FrameworkElement item in InternalChildren) { if (item.Tag is ToolStripStatusLabel lbl && lbl.Spring) { item.Width = springWidth; item.Height = constraint.Height; item.Measure(new Size(item.Width, item.Height)); } } return constraint; } return base.MeasureOverride(constraint); } } }