WPF对齐容器中的项目

时间:2014-08-20 20:45:12

标签: wpf xaml styles border

所以我有简单的xaml:

    <TextBox Grid.Row="0" Grid.Column="1" Margin="10,14,10,5" BorderBrush="Magenta" BorderThickness="1" >
        <TextBox.Text >
            <Binding Path="Name" NotifyOnTargetUpdated="True" UpdateSourceTrigger="PropertyChanged" >
                <Binding.ValidationRules>
                    <DataErrorValidationRule ValidatesOnTargetUpdated="False"  />
                </Binding.ValidationRules>
            </Binding>
        </TextBox.Text>
    </TextBox>

    <Border Grid.Row="1" Grid.Column="1" BorderThickness="1" BorderBrush="Lime" Margin="10, 0, 10, 0">
        <DockPanel  Margin="0"  >
            <Button  Margin="0, 0, 5, 0" Width="100" Content="First" />
            <Button  DockPanel.Dock="Right" Width="100" Content="Second" />
        </DockPanel>
    </Border>

看起来如此:

enter image description here

我想放置按钮,使第一个按钮左对齐,secoond按钮右对齐。但正如您所看到的,第二个按钮和边框右侧之间有一个空格。我怎么能杀掉这个空间?预先感谢!

1 个答案:

答案 0 :(得分:3)

这是因为LastChildFillDockPanel)默认情况下为真,导致该行为。 您可以将其设置为false以解决问题:

<DockPanel  Margin="0" LastChildFill="False">
        <Button  Margin="0, 0, 5, 0" Width="100" Content="First" />
        <Button  DockPanel.Dock="Right" Width="100" Content="Second" />
</DockPanel>