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

标签 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:是否可以在现有面板上再放置一个堆叠面板?

最佳答案

您可能正在寻找 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

关于c# - 如何将两个不同的文本框放入同一级别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20353489/

相关文章:

c# - 验证 Google 凭据

c# - 如何在C#中使用\in字符串

c# - 为什么设置大宽度时边框消失?

visual-studio - 在 Visual Studio 中设置京都内阁

c++ - 是否可以从 Visual Studio 调试器检测 GDI 泄漏?

c# - 如何在 SQL 事件探查器中查看 SQL 查询?

c# - 在 Windows 上,我可以将命名管道用作文件吗?

wpf - 在 xaml 中设置自定义窗口属性

wpf - 如何增加组合框显示项目的填充?

visual-studio - 在 VS 2008 中调试单元测试用例