XAML StackPanel within DockPanel is not Docking Right

DonBaechtel-7903 191 Reputation points
2023-02-22T14:51:58.33+00:00

The XAML StackPanel Name="statusSP" is not DockPanel.Dock="Right"nor HorizontalAlignment="Right". Why? How to get statusSP to align to the Top Right corner of the Window?

<Window x:Class="Cinetique_GUI.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:Cinetique_GUI"
        mc:Ignorable="d"
        Title="Cinetique  GUI" Height="1080" Width="1920" WindowState="Maximized" >
    <Window.Resources>
        <x:Array x:Key="modes" Type="{x:Type local:Mode}">
            <local:Mode Name="Manual"/>
            <local:Mode Name="Teach"/>
            <local:Mode Name="Playback"/>
            <local:Mode Name="Setup"/>
        </x:Array>
    </Window.Resources>
    <DockPanel LastChildFill="False">
        <StackPanel DockPanel.Dock="Top">
            <StackPanel Orientation="Horizontal">
                <Image Source="Images/Logo-White-Crop.png" Stretch="Fill" Margin="5" Width="300" Height="100" HorizontalAlignment="Left"  />
                
                    <Label Content="System Status:" Margin="10,10,10,10" FontSize="30" FontWeight="Bold"/>
                    <Label Name="statusLBL" Content="Ready" Margin="10,10,10,10" FontSize="30" FontWeight="Bold" Foreground="Red"/>
                </StackPanel>
            </StackPanel>
            <StackPanel Orientation="Horizontal">
                <Label Content="Mode:" Margin="10,10,10,10" FontSize="30" FontWeight="Bold"/>
                <ListBox x:Name="selLST" SelectionChanged="PrintText" 
                    SelectionMode="Single" MaxHeight="42"
                    ItemsSource="{Binding}" DataContext="{StaticResource modes}"

                    DisplayMemberPath="Name"
                    ScrollViewer.VerticalScrollBarVisibility="Hidden" 
                         FontSize="28" FontWeight="Bold" Foreground="Red"/>
            </StackPanel>
        </StackPanel>
        <StackPanel DockPanel.Dock="Bottom" Orientation="Horizontal" HorizontalAlignment="Center" Margin="20,20,20,20">
            <Button Content="Stop" Margin="20,10,40,10" FontSize="36" FontWeight="Bold" Width="120"/>
            <Button Content="Quit" Margin="40,10,20,10" FontSize="36" FontWeight="Bold" Width="120"/>
        </StackPanel>
    </DockPanel>
</Window>

Developer technologies | Windows Presentation Foundation
Windows for business | Windows Client for IT Pros | User experience | Other
{count} votes

1 answer

Sort by: Most helpful
  1. Hui Liu-MSFT 48,681 Reputation points Microsoft External Staff
    2023-02-23T02:30:26.67+00:00

    Which StackPanel is your StackPanel Name="statusSP" ?

    If it is the outermost layer of the StackPanel you could refer to the following code. It works fine when HorizontalAlignment="Right" is set.

     <DockPanel LastChildFill="False">
            <StackPanel DockPanel.Dock="Top"   Name="statusSP"  HorizontalAlignment="Right"  >
    
                <Image Source="Images\tu.jfif" Stretch="Fill" Margin="5" Width="300" Height="100" HorizontalAlignment="Left"  />
                <StackPanel Orientation="Horizontal">
                    <Label Content="System Status:" Margin="10,10,10,10" FontSize="30" FontWeight="Bold"/>
                    <Label Name="statusLBL" Content="Ready" Margin="10,10,10,10" FontSize="30" FontWeight="Bold" Foreground="Red"/>
                </StackPanel>
                <StackPanel Orientation="Horizontal">
                    <Label Content="Mode:" Margin="10,10,10,10" FontSize="30" FontWeight="Bold"/>
                    <ListBox SelectionMode="Single" MaxHeight="30"  ItemsSource="{Binding}" DataContext="{StaticResource modes}"
                        ScrollViewer.VerticalScrollBarVisibility="Visible">
                    </ListBox>
                </StackPanel>
            </StackPanel>
            <StackPanel DockPanel.Dock="Bottom" Orientation="Horizontal" HorizontalAlignment="Center" Margin="20,20,20,20">
                <Button Content="Stop" Margin="20,10,40,10" FontSize="36" FontWeight="Bold" Width="120"/>
                <Button Content="Quit" Margin="40,10,20,10" FontSize="36" FontWeight="Bold" Width="120"/>
    
            </StackPanel>
        </DockPanel>
    
    

    The result:

    User's image

    If you still have questions, you can describe which control you want to set.


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.