WPF align items in container

2020-04-21 04:03发布

问题:

So i have thi simple 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>

It looks so:

I would like to place buttons such a way that First button was left-aligned and secoond button was right-aligned. but as you can see there is a space between second button and right side of border. How could i kill off this space? Thank in advance!

回答1:

It's because LastChildFill (of the DockPanel) is true by default causing that behavior. You can set it to false to solve the problem:

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