【问题标题】:How to put two different textboxes into same level?如何将两个不同的文本框放入同一级别?
【发布时间】:2013-12-19 15:33:22
【问题描述】:

正确的 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"/>

你可以有两个文本框,一个位于另一个之下,我需要将它们放在同一级别,我尝试通过构造函数,它在我的特定情况下根本不这样做。为了清楚起见,我添加了图片。

而且不要害怕外国铭文,没关系。

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

UPD:这里所有的 xaml 代码 http://snipt.org/Btff4/Default#expand UPD:是否有可能在现有的堆栈面板上再添加一个?

【问题讨论】:

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


    【解决方案1】:

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

    执行结果:

    更新

    添加允许实现所需布局的 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>
    

    执行结果:

    【讨论】:

    • 可能它应该可以工作,但后来我添加了这样的属性,我的所有对象都消失了。在我回到垂直之后,最终回到了以前的状态/
    • 能否请您发布一个更清晰的 XAML 版本?目前情况如此混乱,我根本无法理解一切是如何运作的。
    • 同时,您可以下载并使用可视化/逻辑树浏览器,例如 WPF Inspector wpfinspector.codeplex.com。您看不到某些对象的事实并不意味着它们不存在。
    • 好的,我终于下载了您的代码,它与您原始问题中发布的代码绝对没有任何共同之处。请注意,它不是 WinForms,因此不能只使用设计器。使用边距定位所有元素将导致您无处可去。
    • 我查看了您发布的最后一部分代码。您将所有元素都放在 TextBlock 中,因此几乎不可能更改布局。我强烈怀疑是否应该遵循这种方法。请参阅我的更新答案,了解如何实现目标的非常简化示例(请注意,这只是可能的解决方案之一)。
    【解决方案2】:

    您可以将StackPanelOrientation="Horizontal" 一起用作容器。比如:

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

    【讨论】:

      【解决方案3】:

      我更喜欢网格定义而不是 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>
      

      【讨论】:

        【解决方案4】:

        网格应该做你需要的:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-07-09
          • 1970-01-01
          • 2015-09-25
          • 2011-07-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-07-08
          相关资源
          最近更新 更多