我怎样才能直接对wpf文本框进行排序?

时间:2014-09-01 17:43:50

标签: c# wpf xaml location

我设计了一个简单的登录窗口,我非常想知道这一点。

嗯,这是我的XAML代码

<Grid ShowGridLines="False">
    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
        <RowDefinition Height="*"/>
        <RowDefinition Height="*"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <DockPanel Grid.Row="0">
        <Label x:Name="label_ID" Height="30"/>
        <TextBox x:Name="textBox_ID" Height="20" TabIndex="0">
            <TextBox.Margin>
                <Thickness Right="5"/>
            </TextBox.Margin>
        </TextBox>
    </DockPanel>
    <DockPanel Grid.Row="1">
        <Label x:Name="label_PW" Height="30"/>
        <PasswordBox x:Name="textBox_PW" Height="20" TabIndex="1">
            <PasswordBox.Margin>
                <Thickness Right="5"/>
            </PasswordBox.Margin>
        </PasswordBox>
    </DockPanel>
    <DockPanel Grid.Row="2">
        <Label x:Name="label_IP" Height="30"/>
        <TextBox x:Name="textBox_IP" Height="20" UndoLimit="2">
            <TextBox.Margin>
                <Thickness Right="5"/>
            </TextBox.Margin>
        </TextBox>
    </DockPanel>
</Grid>

label_ID&#39; s字符串为ID label_PW&#39的字符串是Password label_IP的字符串是&#39; IP&#39;

输出如下:

https://www.dropbox.com/s/k23ft06layp0sl4/1.PNG?dl=0

密码文本框仅短于其他密码。 (我想将另一个文本框缩短为。)

如何修复文本框的位置?

感谢。

3 个答案:

答案 0 :(得分:2)

我认为您的DockPanels并不是真的需要,而是可以在Grid中使用两列:

<Grid ShowGridLines="False">
     <Grid.ColumnDefinitions>
         <ColumnDefinition Width="Auto"/>
         <ColumnDefinition Width="*"/>
     </Grid.ColumnDefinitions>
     <Grid.RowDefinitions>
         <RowDefinition Height="Auto"/>
         <RowDefinition Height="Auto"/>
         <RowDefinition Height="Auto"/>
     </Grid.RowDefinitions>

     <Label Grid.Row="0" Grid.Column="0" x:Name="label_ID" Height="30"/> 
     <TextBox Grid.Row="0" Grid.Column="1" x:Name="textBox_ID" Height="20" TabIndex="0" Margin="5"/>

     <Label Grid.Row="1" Grid.Column="0" x:Name="label_PW" Height="30"/>
     <PasswordBox Grid.Row="1" Grid.Column="1" x:Name="textBox_PW" Height="20" TabIndex="1" Margin="5"/>

     <Label Grid.Row="2" Grid.Column="0" x:Name="label_IP" Height="30"/> 
     <TextBox Grid.Row="2" Grid.Column="1" x:Name="textBox_IP" Height="20" UndoLimit="2"  Margin="5"/>
</Grid>

将第一列宽度设置为Auto可确保文本可见,第二列将占用剩余空间(*)。

它产生这样的输出(我添加了一些测试字符串):

output

答案 1 :(得分:1)

我能想到的最简单的方法是

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="200" />
        <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="30" />
        <RowDefinition Height="30" />
        <RowDefinition Height="30" />
    </Grid.RowDefinitions>
    <Label Name="lblID" Content="ID:" HorizontalAlignment="Right" VerticalAlignment="Center" Width="Auto" Margin="5,2,5,2" />
    <TextBox Name="txtID" HorizontalAlignment="Left" VerticalAlignment="Center" Grid.Column="1" Grid.Row="0" Margin="5,2,0,2" Width="250" Height="25" />
    <Label Name="lblPwd" Content="Password:" HorizontalAlignment="Right" VerticalAlignment="Center" Width="Auto" Grid.Column="0" Grid.Row="1" Margin="5,2,5,2" />
    <PasswordBox Name="pwdBx" HorizontalAlignment="Left" VerticalAlignment="Center" Grid.Column="1" Grid.Row="1" Margin="5,2,0,2" Width="250" Height="25" />
    <Label Name="lblIP" Content="IP:" HorizontalAlignment="Right" VerticalAlignment="Center" Width="Auto" Grid.Column="0" Grid.Row="2" Margin="5,2,5,2" />
    <TextBox Name="txtIP" HorizontalAlignment="Left" VerticalAlignment="Center" Grid.Column="1" Grid.Row="2" Margin="5,2,0,2" Width="250" Height="25" />
</Grid>

答案 2 :(得分:0)

现在我只能考虑使用margin属性:

<UIElement Margin="left, top, right, bottom" />
<Menu Name="EgMenu"
      Margin="20,20,0,20" />

margin属性将帮助您以更轻松的方式定位和调整UI元素的大小,以便您只需查看文本即可直观显示。但是为什么不使用WYSIWYG,稍后通过编辑XAML进行改进。