I need help with the layout of a WPF application.
I've tried different combinations of DockPanel
, StackPanel
, WrapPanel
, Grid
and UniformGrid
.
But I can't seem to align the text of all the controls. I understand the checkboxes won't align due to control size, but the first one should still line up.
How would you arrange these controls?
| GroupBox | GroupBox | |
| -> cbx ->tbx+lbl | TextBox+button |
| -> cbx ->tbx+lbl | ComboBox+button |
| TabControl |
I've been trying to size all controls to default height 23 but that seems to make things worse due to the margins I'm trying to use.
<Grid x:Name="MasterGrid" Margin="10">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<DockPanel Grid.Row="0">
<GroupBox Header="CheckBox" Margin="5 0 0 0">
<StackPanel Margin="5">
<StackPanel.Resources>
<Style TargetType="{x:Type CheckBox}">
<Setter Property="Margin" Value="0 5 0 0"/>
</Style>
</StackPanel.Resources>
<CheckBox Content="XXXX"/>
<CheckBox Content="XXXX"/>
</StackPanel>
</GroupBox>
<GroupBox Header="Label + TextBox" Margin="5 0 0 0">
<StackPanel Grid.IsSharedSizeScope="True">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" SharedSizeGroup="Label"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Label Grid.Column="0" Content="XXXX:" Margin="0 5 0 0" Height="23"/>
<TextBox Grid.Column="1" Text="XXXX" VerticalContentAlignment="Center" Margin="0 5 5 0" Height="23"/>
</Grid>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" SharedSizeGroup="Label"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Label Grid.Column="0" Content="XXXX:" Margin="0 5 0 0"/>
<TextBox Grid.Column="1" Text="XXXX" VerticalContentAlignment="Center" Margin="0 5 5 0" Height="23"/>
</Grid>
</StackPanel>
</GroupBox>
<Grid VerticalAlignment="Center" Margin="0 5 0 0">
<Grid.Resources>
<Style TargetType="{x:Type Button}">
<Setter Property="Height" Value="23"/>
<Setter Property="Margin" Value="5 5 0 0"/>
<Setter Property="Width" Value="75"/>
</Style>
<Style TargetType="{x:Type TextBox}">
<Setter Property="Height" Value="23"/>
<Setter Property="Margin" Value="5 5 0 0"/>
</Style>
<Style TargetType="{x:Type ComboBox}">
<Setter Property="Height" Value="23"/>
<Setter Property="Margin" Value="5 5 0 0"/>
</Style>
</Grid.Resources>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<TextBox Grid.Row="0" Grid.Column="0" Text="XXXXXXXXXX" VerticalContentAlignment="Center"/>
<Button Grid.Row="0" Grid.Column="1" Content="Browse"/>
<Button Grid.Row="1" Grid.Column="1" Content="Load"/>
<ComboBox Grid.Row="1" Grid.Column="0" SelectedItem="XXXXXXXXXX"/>
</Grid>
</DockPanel>
<!-- Imported Data Collection -->
<TabControl Grid.Row="1" Margin="0 5 0 5"/>
</Grid>
I've gone through a ton of existing questions and experimented with each answer (couple examples)
- Understanding Uniform Grid control
- WPF layout problem with Grid.IsSharedSizeScope and ItemsControl.ItemTemplate
- How do I layout a form in WPF using grid or other controls for maintainability
(Background for future question searchers: tool is for browsing through log files. Select the folder path -> combobox is populated with file names -> select file to load into tabcontrol. The two groupboxes contain options for how to handle different file formats)