尝试在xaml中使元素对齐正确

时间:2018-02-01 17:35:02

标签: wpf xaml

我试图让第一个标签和文本框与屏幕左侧对齐,第二个标签和文本框与屏幕右侧对齐。我在这里错过了什么?第二组控件不会与右侧对齐。

提前致谢!

round

1 个答案:

答案 0 :(得分:0)

外线StackPanel是你的问题。请改用Grid。您的问题实际上是Aligning controls on both left and right side in a stack panel in WPF

的副本

以下是Grid的代码:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto"></ColumnDefinition>
    </Grid.ColumnDefinitions>
    <Grid Grid.Column="0">
        <Grid.ColumnDefinitions>
           <ColumnDefinition Width="Auto" />
           <ColumnDefinition Width="*" />
           <ColumnDefinition Width="Auto" />
        </Grid.ColumnDefinitions>
        <StackPanel Grid.Column="0" Orientation="Horizontal">
                <Label Content="Active Profile"></Label>
                <TextBox Name="activeProfileName" Width="100" Height="20"></TextBox>
        </StackPanel>
        <StackPanel Grid.Column="2" Orientation="Horizontal" HorizontalAlignment="Right" >
            <Label Content="Create A New Profile"                        
                    HorizontalAlignment="Right"></Label>
            <TextBox Name="activeProfileNamey"
                       Width="100" Height="20"
                       HorizontalAlignment="Right"></TextBox>
        </StackPanel>
    </Grid>
</Grid>