2014-09-01 42 views
1

我正在設計一個簡單的登錄窗口,我非常想知道這一點。如何直接對wpf文本框進行排序?

好了,這是我的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的字符串是ID label_PW的字符串是Password label_IP的字符串是 'IP'

和輸出是這樣的:

https://www.dropbox.com/s/k23ft06layp0sl4/1.PNG?dl=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

+0

哇多麼完美的答案。非常感謝。 – Lee 2014-09-01 18:19:10

+0

不客氣。只是一個小的評論:至少我猜,UndoLimit =「2」看起來像是一個「錯字」,應該是TabIndex =「2」。 – qqbenq 2014-09-01 19:08:34

+0

是的,我看到:)謝謝。 – Lee 2014-09-02 05:33:39

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

非常感謝。 – Lee 2014-09-01 18:19:59

0

現在我只能想到用margin屬性中的:

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

保證金屬性將幫助您以輕鬆的方式定位和調整UI元素的大小,以便您可以直觀地查看文本。但爲什麼不使用所見即所得,並稍後通過編輯XAML來完善。

相關問題