Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 441
Michel Keijzers Avatar asked Aug 27 '12 23:08

Michel Keijzers


People also ask

Which panel in WPF is well suited to place elements in an ordered and aligned manner?

The Panel ClassDerived Panel elements are used to position and arrange elements in Extensible Application Markup Language (XAML) and code.

How do you add a space between two controls in WPF?

If you do not use (for some reason) Button's Margin property, you can put transparent Separator (Transparent background color) with desired Width (or/and Height) between your controls (Buttons in your case). Save this answer. Show activity on this post. Save this answer.

What is content alignment in WPF?

Content Alignment The content of content controls in WPF is dealt using various properties. These two properties are HorizontalContentAlignment and VerticalContentAlignment.

What is stack panel?

StackPanel is a layout panel that arranges child elements into a single line that can be oriented horizontally or vertically. By default, StackPanel stacks items vertically from top to bottom in the order they are declared. You can set the Orientation property to Horizontal to stack items from left to right.


1 Answers

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> 
like image 172
H.B. Avatar answered Sep 19 '22 15:09

H.B.