using System.Drawing; namespace System.Windows.Forms { public partial class ContainerControl : Control { protected System.Windows.Controls.ScrollViewer scrollViewer { get; private set; } protected System.Windows.Controls.Panel container { get; private set; } public AutoScaleMode AutoScaleMode { get; set; } public SizeF AutoScaleDimensions { get; set; } public bool AutoScroll { get => scrollViewer.HorizontalScrollBarVisibility == Windows.Controls.ScrollBarVisibility.Auto; set { scrollViewer.HorizontalScrollBarVisibility = scrollViewer.VerticalScrollBarVisibility = value ? Windows.Controls.ScrollBarVisibility.Auto : Windows.Controls.ScrollBarVisibility.Disabled; if (value) { scrollViewer.Content = container; (control as System.Windows.Controls.ContentControl).Content = scrollViewer; scrollViewer.ScrollChanged += (s, e) => OnScroll(new ScrollEventArgs(ScrollEventType.EndScroll, 0)); } } } // do not set WPF Padding for container controls. It has different behaviour compared to WinForms private Padding padding; public override Padding Padding { get => padding; set { padding = value; UpdateLayout(); } } public event ScrollEventHandler Scroll; protected override void ScaleCore(float dx, float dy) { base.ScaleCore(dx, dy); ScaleInternals(dx, dy); } internal void ScaleInternals(float dx, float dy) { AutoScaleDimensions = new Drawing.SizeF(AutoScaleDimensions.Width * dx, AutoScaleDimensions.Height * dy); Padding = new Padding((int)(Padding.Left * dx), (int)(Padding.Top * dy), (int)(Padding.Right * dx), (int)(Padding.Bottom * dy)); DefaultPadding = new Padding((int)(DefaultPadding.Left * dx), (int)(DefaultPadding.Top * dy), (int)(DefaultPadding.Right * dx), (int)(DefaultPadding.Bottom * dy)); } protected virtual void OnScroll(ScrollEventArgs e) => Scroll?.Invoke(this, e); internal override void AddChild(Control child) { // mimic winforms behavior: add actually inserts a child at the beginning of the list container.Children.Insert(0, child.control); } internal override void RemoveChild(Control child) { container.Children.Remove(child.control); } internal override void SetChildIndex(Control child, int index) { container.Children.Insert(container.Children.Count - index, child.control); } protected void SetContentControl(System.Windows.Controls.ContentControl control) => SetContentControl(control, control); protected void SetContentControl(System.Windows.Controls.Control control, System.Windows.Controls.ContentControl contentControl) => SetContentControl(control, contentControl, new System.Windows.Controls.Grid()); protected void SetContentControl(System.Windows.Controls.Control control, System.Windows.Controls.ContentControl contentControl, System.Windows.Controls.Panel container) { this.container = container; container.FlowDirection = FlowDirection.LeftToRight; scrollViewer = new(); scrollViewer.FocusVisualStyle = null; contentControl.Content = container; SetControl(control); container.Tag = this; } internal override void AfterDpiChange(float rescale) { ScaleInternals(rescale, rescale); base.AfterDpiChange(rescale); } public ContainerControl() { SetContentControl(new OwnerDrawControl(this)); } } }