Aligning controls on both left and right side in a stack panel in WPF

WpfXamlLayoutAlignmentStackpanel

Wpf Problem Overview


I have the following code:

<DockPanel>
    <StackPanel DockPanel.Dock="Top" Orientation="Horizontal">
        <RadioButton Content="_Programs" 
                    IsChecked="{Binding Path=ProgramBanksSelected}" IsEnabled="{Binding Path=ProgramsEnabled}" Margin="8" />
        <StackPanel>
            <Label Content="Master" Height="28" Name="MasterFileStatus" VerticalContentAlignment="Center"/>
        </StackPanel>
    </StackPanel>
    ...

The radio button should be placed on the left side in the stack panel (I removed some buttons for not cluttering the example) and the label (which I put temporarily in a nested StackPanel) should be on the right side.

I tried already lots of combinations of alignments but I cannot get the label on the right side. What should I add to accomplish this?

Wpf Solutions


Solution 1 - Wpf

Just do not use a StackPanel, StackPanels stack. They do, for obvious reasons, not allow alignment in the direction in which they stack. Use a Grid, with column definitions like so:

<Grid.ColumnDefinitions>
	<ColumnDefinition Width="Auto" />
	<ColumnDefinition Width="*" />
	<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>

Solution 2 - Wpf

Even though this is old, should someone come across this like I did, here's a simple solution.

Make a new grid and inside that grid put two stack panels with different Horizontal Alignment.

<Grid>
    <StackPanel Orientation="Horizontal" HorizontalAlignment="Left">
        <!--Code here-->
    </StackPanel>
    <StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
        <!--Code here-->
    </StackPanel>
</Grid>

The possible issue is that now without extra handling the two could overlap with each other.

Solution 3 - Wpf

As you have set the StackPanel's orientation to Horizontal, the HorizontalAlignment property won't work on child-elements. You can keep the StackPanel if you need additional controls, though I would recommend switching to a Grid (among other things) to build the layout you want.

Also, the Grid will allow you to control the actual width of each column:

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="50" />
        <ColumnDefinition Width="150" />
    </Grid.ColumnDefinitions>

    <RadioButton
        Grid.Column="0"
        ...
    />

    <Label
        Grid.Column="1"
        ...
    />
</Grid>

Solution 4 - Wpf

User @pasx is right. You should use DockPanel and dock the RadioButton to the left side, and your StackPanel with the label to the right side.

<DockPanel>

    <DockPanel 
        DockPanel.Dock="Top" 
        LastChildFill="False" >

        <RadioButton 
            DockPanel.Dock="Left" 
            Content="_Programs" 
            IsChecked="{Binding Path=ProgramBanksSelected}"
            IsEnabled="{Binding Path=ProgramsEnabled}" 
            Margin="8" />

        <StackPanel
            DockPanel.Dock="Right">

            <Label 
                Content="Master" 
                Height="28" 
                Name="MasterFileStatus" 
                VerticalContentAlignment="Center"/>

        </StackPanel>

    </DockPanel>
    ...

Solution 5 - Wpf

Use the Stackpanel orientation type in a nested fashion as shown in the sample. The Grid Width is set to a parent width in order to achieve full width.

<StackPanel x:Name="stackBlah" Orientation="Vertical">
	<StackPanel Orientation="Horizontal">
		<Grid Width="{Binding ActualWidth, ElementName=stackBlah}" >
			<Grid.ColumnDefinitions>
				<ColumnDefinition Width="*" />
				<ColumnDefinition Width="Auto" />
			</Grid.ColumnDefinitions>
			<Grid.RowDefinitions>
				<RowDefinition />
			</Grid.RowDefinitions>

			<StackPanel Grid.Column="0" HorizontalAlignment="Left" >
				<Button Content="Some button" />
			</StackPanel>
			<StackPanel Grid.Column="1" HorizontalAlignment="Right">
				<ToggleSwitch Header="Some toggle" AutomationProperties.Name="ToggleNotifications"/>
			</StackPanel>
		</Grid>
	</StackPanel>
</StackPanel>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMichel KeijzersView Question on Stackoverflow
Solution 1 - WpfH.B.View Answer on Stackoverflow
Solution 2 - WpfChrisView Answer on Stackoverflow
Solution 3 - WpfnewfurnitureyView Answer on Stackoverflow
Solution 4 - WpfAlex34758View Answer on Stackoverflow
Solution 5 - WpfGregory BolognaView Answer on Stackoverflow