A.D.
A.D.

Reputation: 1116

WPF UserControls display left when I specify HorizontalAlignment="Right"

This is my WPF UserControl code reduced to the relevant part:

<UserControl x:Class="AKPS.View.UserCalibWindow"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
           >


        <StackPanel  Orientation="Horizontal"  HorizontalAlignment="Right"  Width="1600" >
        <Button   Height="45" Width="100" />
        <Button   Height="45" Width="100" />                
    </StackPanel>

Why do the 2 buttons display on the left?

Upvotes: 0

Views: 369

Answers (3)

Clint
Clint

Reputation: 6220

HorizontalAlignment controls the container's alignment.

Try HorizontalContentAlignment instead.


As Abin indicated, in my haste I'd forgotten StackPanel doesn't have a HorizontalContentAlignment property.

You could achieve similar functionality with a DockPanel though, or use Abin's solution.

Upvotes: 1

Abin
Abin

Reputation: 2956

You are having a Width for your StackPanel removing that will align your stack panel to right with your controls in it.

<StackPanel  Orientation="Horizontal"  HorizontalAlignment="Right"  >
    <Button Height="45" Width="100" />
    <Button Height="45" Width="100" />
</StackPanel>

Upvotes: 3

plusheen
plusheen

Reputation: 1426

A StackPanel does exactly that - it stacks elements together, in your case, horizontally. Your HorizontalAlignment is referring to the stack panel, which will shift it to the right, not the buttons inside them.

Instead, perhaps try using a Grid instead of a StackPanel, and then placing your StackPanel (with no Width element set) inside it.

Upvotes: 1

Related Questions