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

时间:2015-12-22 12:09:01

标签: c# wpf xaml stackpanel dockpanel

现在我有以下内容:

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

结果如下:

How it currently is

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

像这样:

How I want it to be

1 个答案:

答案 0 :(得分:1)

您可以通过使用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>

enter image description here