Question

My app needs to have items in be aligned by the user; left, right and center. I am having an issue formating the app to handle this, meaning that application can either align left or right or center but not all three. How can I get the items for example 5 buttons to be aligned left, right and center changed via a button press (I should know how to do this once I am able to actual format in XMLA)?

My first thought would be do this with a dock panel;

<DockPanel MinWidth="500" Background="LightGray" Margin="20" LastChildFill="False">
   <Button Margin="10" DockPanel.Dock="Left" >one</Button>
   <Button Margin="10" DockPanel.Dock="Left">two</Button>
   <Button Margin="10" DockPanel.Dock="Left">three</Button>
   <Button Margin="10" DockPanel.Dock="Left">four</Button>
   <Button Margin="10" DockPanel.Dock="Left">five</Button>
</DockPanel>

This works great for Left and right alignment. The problem is that there is no DockPanel.Dock="Center"

Next tried a Grid;

<Grid Background="LightGray" MinWidth="500">
    <Button HorizontalAlignment="Left" Margin="10,10,10,10">one</Button>
    <Button HorizontalAlignment="Left" Margin="10,10,10,10">two</Button>
    <Button HorizontalAlignment="Left" Margin="10,10,10,10">three</Button>
    <Button HorizontalAlignment="Left" Margin="10,10,10,10">four</Button>
    <Button HorizontalAlignment="Left" Margin="10,10,10,10">five</Button>
</Grid>

Problem is that this happens;

enter image description here

edit: Apparently I didn't make the my question clear enough.

edit2: @Henka Programmer suggestion below did not work. First I believe he meant HorizontalAlignment not VerticalAlignment. With this code;

<StackPanel Orientation="Horizontal"  HorizontalAlignment="Right" Background="LightGray" MinWidth="500">
<Button Content="btn 01" />
<Button Content="btn 02" />
<Button Content="btn 03" />
<Button Content="btn 0" />
</StackPanel>

Gives this result;

enter image description here

Changing HorizontalAlignment="Right" to HorizontalAlignment="Center" makes no difference

Was it helpful?

Solution

The horizontal alignment property on the StackPanel is going to align the StackPanel relative to its parent element. This works just fine:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="1000">
    <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Top">
        <Button Content="Button 1" />
        <Button Content="Button 2" />
        <Button Content="Button 3" />
        <Button Content="Button 4" />
        <Button Content="Button 5" />
    </StackPanel>
</Window>

OTHER TIPS

i think you mean this:

// ...
<Grid>

<StackPanel Orientation="Horizontal" VerticalAlignment="Center">
<Button Content="btn 01" />
<Button Content="btn 02" />
<Button Content="btn 03" />
<Button Content="btn 0" />
</StackPanel>

</Grid>
// ...

and you can change the alignment of StackPanel with:

  • VeriticalAlignment="Center"
  • VeriticalAlignment="Right"
  • VeriticalAlignment="Left"

and the StackPanel Orientation :

  • Vertical

  • Horizontal

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top