如何在 WPF 中相互设置多个按钮、文本块和文本框?

How to set multiple buttons, textblocks and textboxes underneath each other in WPF?

现在我有以下内容:

<DockPanel Height="150">

    <Button DockPanel.Dock="Right" Padding="5" Margin="5 0 5 0" FontWeight="Bold" Content="Submit" Click="Button_Click"/>
    <TextBlock TextAlignment="Left" Padding="5" Text="DVM Positive Readout" />

    <Border Height="25" BorderBrush="Black" BorderThickness="1" DockPanel.Dock="Top">
        <TextBox HorizontalAlignment="Stretch" VerticalAlignment="Center" x:Name="DVM_Positive_ReadOut" LostKeyboardFocus="DVM_Positive_ReadOut_LostKeyboardFocus" />
    </Border>

    <Border Height="25" BorderBrush="Black" BorderThickness="1" DockPanel.Dock="Top">
        <TextBox HorizontalAlignment="Stretch" VerticalAlignment="Center" x:Name="DVM_Negavtive_ReadOut" LostKeyboardFocus="DVM_Negative_ReadOut_LostKeyboardFocus" />
    </Border>

</DockPanel>

这导致:

我想要两个提交按钮和两个文本块。文本框正确堆叠,但如果它们居中就更好了。

像这样:

您可以通过使用 StackPanel 及其水平或垂直属性来实现此目的:

<StackPanel Orientation="Vertical">
        <StackPanel Orientation="Horizontal">
            <TextBlock TextAlignment="Left"
                       Padding="5"
                       Text="DVM Positive Readout" 
                       Width="150"/>
            <Border Height="25"
                    BorderBrush="Black"
                    BorderThickness="1"
                    Width="150"
                    DockPanel.Dock="Top">
                <TextBox HorizontalAlignment="Stretch"
                         VerticalAlignment="Center"
                         x:Name="DVM_Positive_ReadOut"
                         LostKeyboardFocus="DVM_Positive_ReadOut_LostKeyboardFocus" />
            </Border>
            <Button DockPanel.Dock="Right"
                    Padding="5"
                    Margin="5 0 5 0"
                    FontWeight="Bold"
                    Content="Submit"
                    Click="Button_Click" />
        </StackPanel>

        <StackPanel Orientation="Horizontal">
            <TextBlock TextAlignment="Left"
                       Padding="5"
                       Text="DVM Negative Readout"
                       Width="150" />
            <Border Height="25"
                    BorderBrush="Black"
                    BorderThickness="1"
                    Width="150"
                    DockPanel.Dock="Top">
                <TextBox HorizontalAlignment="Stretch"
                         VerticalAlignment="Center"
                         x:Name="DVM_Negavtive_ReadOut"
                         LostKeyboardFocus="DVM_Negative_ReadOut_LostKeyboardFocus" />
            </Border>
            <Button DockPanel.Dock="Right"
                    Padding="5"
                    Margin="5 0 5 0"
                    FontWeight="Bold"
                    Content="Submit"
                    Click="Button_Click" />
        </StackPanel>
    </StackPanel>