【问题标题】:Trying to get element alignment correct in xaml试图在 xaml 中正确对齐元素
【发布时间】:2018-07-12 03:00:41
【问题描述】:

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

提前致谢!

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

【问题讨论】:

标签: wpf xaml


【解决方案1】:

外部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>

【讨论】:

  • 代码不起作用。它与我发布的代码具有相同的行为。
  • 实际上,如果将列的列定义从'Auto'更改为'*',它会按预期工作。谢谢zmechanic!!!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-13
  • 2020-10-19
  • 1970-01-01
  • 2020-05-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多