Vertical align the data control

This commit is contained in:
Thorsten Sommer 2022-07-27 21:56:59 +02:00
parent fa74291e39
commit 8e21ddffc9
Signed by: tsommer
GPG Key ID: 371BBA77A02C0108

View File

@ -17,17 +17,25 @@ public partial class Setting : UserControl
this.labelIcon.Image = settingMetaData.Icon; this.labelIcon.Image = settingMetaData.Icon;
this.labelSettingName.Text = settingMetaData.SettingName(); this.labelSettingName.Text = settingMetaData.SettingName();
this.labelExplanation.Text = settingMetaData.SettingExplanation(); this.labelExplanation.Text = settingMetaData.SettingExplanation();
var dataControl = settingMetaData.SetupDataControl();
this.tableLayout.Controls.Add(dataControl, 2, 0);
// Calculate the needed height of the explanation label when the parent window is resized: // Ensure, that this data control is vertical centered by calculating the needed margin, considering the outer size of the table layout:
var margin = (this.tableLayout.GetRowHeights().First() - dataControl.Height) / 2f;
dataControl.Margin = new Padding(0, (int) margin, 0, (int)margin);
// Calculate the needed height of the explanation label & centering of the data control when the parent window is resized:
this.tableLayout.Resize += (sender, args) => this.tableLayout.Resize += (sender, args) =>
{ {
// Adjust the height of the parent controls (table & user control): // Adjust the height of the parent controls (table & user control):
this.tableLayout.Height = Math.Max((int)this.labelExplanation.CreateGraphics().MeasureString(this.labelExplanation.Text, this.labelExplanation.Font, new SizeF(this.labelExplanation.Width, 1000)).Height, 66); this.tableLayout.Height = Math.Max((int)this.labelExplanation.CreateGraphics().MeasureString(this.labelExplanation.Text, this.labelExplanation.Font, new SizeF(this.labelExplanation.Width, 1000)).Height, 66);
this.Height = this.tableLayout.Height + this.tableLayout.Margin.Vertical; this.Height = this.tableLayout.Height + this.tableLayout.Margin.Vertical;
// Ensure, that this data control is vertical centered by calculating the needed margin, considering the outer size of the table layout:
var margin = (this.tableLayout.GetRowHeights().First() - dataControl.Height) / 2f;
dataControl.Margin = new Padding(0, (int) margin, 0, (int)margin);
}; };
var dataControl = settingMetaData.SetupDataControl();
this.tableLayout.Controls.Add(dataControl, 2, 0);
} }
private readonly record struct SettingUIData( private readonly record struct SettingUIData(