如何将两个不同的文本框放入同一级别?

时间:2013-12-03 14:13:37

标签: c# wpf visual-studio visual-studio-2013

正确的xaml代码看起来像这样:

mc:Ignorable="d" d:DesignWidth="363" Height="628">
    <Grid Style="{StaticResource ContentRoot}">
        <ScrollViewer Margin="0,-105,-9,0">
            <StackPanel MinWidth="200" HorizontalAlignment="Left" Width="474" Height="459">


            </InlineUIContainer>
            <Run Language="ru-ru"/>
            <LineBreak/>
            <Run Language="ru-ru" Text="Num1"/>
            <LineBreak/>
            <InlineUIContainer>
                <TextBox d:DesignUseLayoutRounding="True" 
                         Width="160" 
                         UseLayoutRounding="True" 
                         Text="Enter num" 
                         TextWrapping="Wrap" 
                         x:Name="TxtBlock_numRequest"
                         InputMethod.IsInputMethodEnabled="False" 
                         Height="23" 
                         AutoWordSelection="True"/>
            </InlineUIContainer>
            <LineBreak/>
            <Run Language="ru-ru"/>
            <LineBreak/>
            <Run Language="ru-ru" Text="ID"/>
            <Run Text="    "/>
            <LineBreak/>
            <InlineUIContainer>
                <TextBox Width="160" 
                         Text="Enter num" 
                         TextWrapping="Wrap" 
                         x:Name="TxtBlock_IDWork" 
                         Height="23"/>

正如你可以有两个文本框,它们位于另一个下面,我需要将它们放在同一级别,我尝试通过构造函数,它只是在我的特定情况下不这样做。要清除我已添加图片。

Q

不要害怕外国铭文,这无关紧要。 enter image description here

我只是不知道如何解决这个问题,我认为它与StackPanel有关。有什么想法吗?

UPD:此处的所有xaml代码http://snipt.org/Btff4/Default#expand UPD:可能有可能再添加一个堆栈面板吗?

4 个答案:

答案 0 :(得分:8)

您可能正在寻找Orientation属性:

<StackPanel MinWidth="200" HorizontalAlignment="Left" Width="474" Height="459" Orientation="Horizontal">

<强>更新

添加一个非常基本的示例来说明StackPanel的工作原理:

<Window x:Class="WpfTestBench.Stackpanel"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" SizeToContent="WidthAndHeight">
    <StackPanel Orientation="Horizontal" Margin="10">
        <TextBox Text="First textbox" Height="20" Margin="5, 0" />
        <TextBox Text="Second textbox" Height="20" />
    </StackPanel>
</Window>

执行结果:

Textboxes

<强>更新

添加允许实现所需布局的XAML:

<Window x:Class="WpfTestBench.Stackpanel"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Sample layout" SizeToContent="Height" Width="400">
    <Grid Margin="10">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="50" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>

        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition />
        </Grid.RowDefinitions>

        <Label Grid.ColumnSpan="3" Grid.Row="0" Content="Задайте параметры заявок" FontWeight="Bold" FontSize="16" />

        <Grid Grid.Column="0" Grid.Row="1">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="10" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>

            <TextBlock Grid.Row="0" Text="номер заявки" />
            <TextBox Grid.Row="1" Text="Введите число" />

            <TextBlock Grid.Row="3" Text="приоритет заявки" />
            <ComboBox Grid.Row="4" SelectedIndex="0">
                <ComboBoxItem Content="Высокий" />
                <ComboBoxItem Content="Средний" />
                <ComboBoxItem Content="Низкий" />
            </ComboBox>
        </Grid>

        <Grid Grid.Column="2" Grid.Row="1">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="10" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>

            <TextBlock Grid.Row="0" Text="идентификатор вида работы" />
            <TextBox Grid.Row="1" Text="Введите число" />
            <TextBlock Grid.Row="3" Text="номер траектории" />
            <TextBox Grid.Row="4" Text="Введите число" />
        </Grid>
    </Grid>
</Window>

执行结果:

Desired layout

答案 1 :(得分:4)

您可以将StackPanelOrientation="Horizontal"一起用作容器。类似的东西:

<StackPanel Orientation="Horizontal">
    <StackPanel>
        <TextBlock>Label</TextBlock>
        <TextBox Width="150"/>
    </StackPanel>
    <StackPanel>
        <TextBlock>Label 2</TextBlock>
        <TextBox Width="150"/>
    </StackPanel>
</StackPanel>

答案 2 :(得分:1)

我更喜欢网格定义而不是StakPanel。您可以使用该方法轻松设计应用程序,只需在许多较小区域中切割每个区域。使用Auto*来定义比例。

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <Grid Grid.Row="0">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <Label Content="Label" Grid.Row="0" Grid.Column="0" HorizontalAlignment="Center" />
        <Label Content="Label" Grid.Row="0" Grid.Column="1" HorizontalAlignment="Center" />
        <TextBox Text="Text here !" Grid.Row="1" Grid.Column="0" />
        <TextBox Text="Text here !" Grid.Row="1" Grid.Column="1" />
    </Grid>
    <Grid Grid.Row="1">
        <Label Content="I need this kind of location" HorizontalAlignment="Center" VerticalAlignment="Center"/>
    </Grid>
</Grid>

答案 3 :(得分:1)

网格应该做你需要的:

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition></ColumnDefinition>
        <ColumnDefinition></ColumnDefinition>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="40"></RowDefinition>
        <RowDefinition Height="40"></RowDefinition>
    </Grid.RowDefinitions>
    <TextBlock   Name="lblTextBlock1" Text="Text Block" VerticalAlignment="Center" HorizontalAlignment="Center" />
    <TextBox Text="TextBlock1" Grid.Row="1"  Margin="5,0,0,0" MaxHeight="20"  MinWidth="100" MaxLength="4" MaxWidth="40"></TextBox>

    <TextBlock Grid.Column="1"   Name="lblTextBlock2" Text="Text Block" VerticalAlignment="Center" HorizontalAlignment="Center" />
    <TextBox Grid.Column="1"  Text="TextBlock2" Grid.Row="1"  Margin="5,0,0,0" MaxHeight="20"  MinWidth="100" MaxLength="4" MaxWidth="40"></TextBox>
</Grid>