2017-04-23 91 views
-1

我的XAML代碼後:文本框改變其位置添加第二個文本框

<Grid> 
    <Grid.Background> 
     <ImageBrush ImageSource="background.jpg"/> 
    </Grid.Background> 

    <TextBlock x:Name="textBlock" HorizontalAlignment="Left" Height="52" Margin="10,10,0,0" TextWrapping="Wrap" Text="Welcome to University Portal" VerticalAlignment="Top" Width="456" FontSize="24" FontWeight="Bold" FontStyle="Italic" FontFamily="SimSun" TextAlignment="Center"/> 
    <TextBlock x:Name="textBlock_Copy" HorizontalAlignment="Left" Height="17" Margin="10,62,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="456" FontWeight="Bold" FontStyle="Italic" FontFamily="SimSun"><Run Text=" * Select your Domain."/></TextBlock> 
    <Button x:Name="teacher_btn" Content="Teacher" HorizontalAlignment="Left" VerticalAlignment="Top" Width="150" Height="50" Margin="10,89,0,0" FontFamily="SimSun" FontSize="22" FontWeight="Bold" FontStyle="Italic" Click="teacher_btn_Click"> 
     <Button.BorderBrush> 
      <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"> 
       <GradientStop Color="Black" Offset="0"/> 
       <GradientStop Color="White" Offset="1"/> 
      </LinearGradientBrush> 
     </Button.BorderBrush> 
     <Button.Background> 
      <ImageBrush/> 
     </Button.Background> 
    </Button> 
    <Button x:Name="student_btn" Content="Student" HorizontalAlignment="Left" VerticalAlignment="Top" Width="150" Margin="165,89,0,0" Height="50" FontFamily="SimSun" FontSize="22" FontWeight="Bold" FontStyle="Italic" Click="student_btn_Click"> 
     <Button.BorderBrush> 
      <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"> 
       <GradientStop Color="Black" Offset="0"/> 
       <GradientStop Color="White" Offset="1"/> 
      </LinearGradientBrush> 
     </Button.BorderBrush> 
     <Button.Background> 
      <ImageBrush/> 
     </Button.Background> 
    </Button> 

    <Button x:Name="exit_btn" Content="Exit" VerticalAlignment="Top" Margin="320,89,0,0" Height="50" FontWeight="Bold" FontStyle="Italic" FontSize="22" FontFamily="SimSun" HorizontalAlignment="Left" Width="150" Click="exit_btn_Click"> 
     <Button.BorderBrush> 
      <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"> 
       <GradientStop Color="Black" Offset="0"/> 
       <GradientStop Color="White" Offset="1"/> 
      </LinearGradientBrush> 
     </Button.BorderBrush> 
     <Button.Background> 
      <ImageBrush/> 
     </Button.Background> 
    </Button> 


    <Grid Name="container" Margin="10,144,0,0" Height="auto" Width="auto" VerticalAlignment="Top" HorizontalAlignment="Left" /> 

</Grid> 

我有一個Grid控制和我要補充幾個TextBox動態。它適用於單個TextBox,但是當我添加第二個TextBox第一個改變它的位置。並且在加入Button之後,TextBox都改變了他們的立場。這是我的代碼。

  TextBlock name_block = new TextBlock(); 
      name_block.Text = "Enter your ID : "; 
      name_block.Margin = new Thickness(5, 8, 0, 0); 
      container.Children.Add(name_block); 

      TextBox name_box = new TextBox(); 
      name_box.Width = 200; 
      name_box.Height = 25; 
      name_box.Margin = new Thickness(150, 5, 1, 1); 
      container.Children.Add(name_box); 


      TextBlock pass_block = new TextBlock(); 
      pass_block.Text = "Enter your password : "; 
      pass_block.Margin = new Thickness(5, 78, 0, 0); 
      container.Children.Add(pass_block); 

      PasswordBox pass_box = new PasswordBox(); 
      pass_box.Width = 200; 
      pass_box.Height = 25; 
      pass_box.Margin = new Thickness(150, 75, 0, 0); 
      container.Children.Add(pass_box); 

      Button login_btn = new Button(); 
      login_btn.Content = "Login"; 
      login_btn.Height = 25; 
      login_btn.Margin = new Thickness(150, 150, 0, 0); 
      container.Children.Add(login_btn); 

      login_btn.Click += Teacher_Login_btn_Click; 

連接輸出的畫面:name_box should be in the front of name_block

+0

沒有好的[MCVE]認爲可靠重現問題,這是不可能說爲什麼物體移動。很顯然,你的'容器'是一種能夠讓孩子放棄孩子的方式,讓孩子們可以讓現有的孩子移動。總之,你不應該在代碼背後操縱UI。我無法提供更具體的說法,只能說您應該在XAML中聲明UI並將視圖模型(或視圖模型)中的相關數據屬性綁定到XAML對象。 –

+0

你想如何放置控件?垂直一個接着一個? – Krishna

+0

'TextBox'應該放在'TextBlock'的前面。 – Jazab

回答

1

更新答:

OK,你需要3個元素添加到您的文本塊,然後在你的密碼框調整保證金。

寬度需要分配給您的文本塊,但您可能會遇到您的默認文本塊行爲設置水平對齊可能居中,因此您需要將其設置爲.Left。

然後在您的密碼框中調整您的保證金,這樣它就不會被推下另一行,您應該很好,因爲我已經用上述更新進行了測試以確認。

這就是爲什麼下面的例子更容易處理,有更多的代碼佈局在更新後的代碼是一個非常頭痛的問題。


通過設置列和行和知名度需要選擇改善設計佈局:

設置你的網格列和行,並使用大小來處理您的佈局。然後,您的利潤率可以在需要時保持差距。

然後,您可以將您的容器網格與列和行並佈局您的XAML並設置網格摺疊或隱藏(取決於您的需要)。然後在您的按鈕上單擊,您可以更改可見性,而不是通過編程方式添加元素。

下面是經編輯的版本:(頂格並不真正需要分配,但列遵循流)

<Grid> <!--Notice I added columns and rows to set layout instead of relaying on your margins--> 
     <Grid.Background> 
      <ImageBrush ImageSource="background.jpg" Opacity="75"/> 
     </Grid.Background> 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition Width="auto"/> 
     </Grid.ColumnDefinitions> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="40"/> 
      <RowDefinition Height="25"/> 
      <RowDefinition Height="75"/> 
      <RowDefinition Height="auto"/> 
     </Grid.RowDefinitions> 
     <TextBlock x:Name="textBlock" Grid.Column="0" Grid.Row="0" HorizontalAlignment="Left" Height="52" Margin="10,10,0,0" TextWrapping="Wrap" Text="Welcome to University Portal" VerticalAlignment="Top" Width="456" FontSize="24" FontWeight="Bold" FontStyle="Italic" FontFamily="SimSun" TextAlignment="Center"/> 
     <TextBlock x:Name="textBlock_Copy" Grid.Column="0" Grid.Row="1" HorizontalAlignment="Left" Height="17" Margin="5,5,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="456" FontWeight="Bold" FontStyle="Italic" FontFamily="SimSun"><Run Text=" * Select your Domain."/></TextBlock> 
     <Button x:Name="teacher_btn" Grid.Column="0" Grid.Row="2" Content="Teacher" HorizontalAlignment="Left" VerticalAlignment="Top" Width="150" Height="50" Margin="10,9,0,0" FontFamily="SimSun" FontSize="22" FontWeight="Bold" FontStyle="Italic" Click="teacher_btn_Click"> 
      <Button.BorderBrush> 
       <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"> 
        <GradientStop Color="Black" Offset="0"/> 
        <GradientStop Color="White" Offset="1"/> 
       </LinearGradientBrush> 
      </Button.BorderBrush> 
      <Button.Background> 
       <ImageBrush/> 
      </Button.Background> 
     </Button> 
     <Button x:Name="student_btn" Grid.Column="0" Grid.Row="2" Content="Student" HorizontalAlignment="Left" VerticalAlignment="Top" Width="150" Margin="165,9,0,0" Height="50" FontFamily="SimSun" FontSize="22" FontWeight="Bold" FontStyle="Italic" Click="student_btn_Click"> 
      <Button.BorderBrush> 
       <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"> 
        <GradientStop Color="Black" Offset="0"/> 
        <GradientStop Color="White" Offset="1"/> 
       </LinearGradientBrush> 
      </Button.BorderBrush> 
      <Button.Background> 
       <ImageBrush/> 
      </Button.Background> 
     </Button> 
     <Button x:Name="exit_btn" Grid.Column="0" Grid.Row="2" Content="Exit" VerticalAlignment="Top" Margin="320,9,0,0" Height="50" FontWeight="Bold" FontStyle="Italic" FontSize="22" FontFamily="SimSun" HorizontalAlignment="Left" Width="150" Click="exit_btn_Click"> 
      <Button.BorderBrush> 
       <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"> 
        <GradientStop Color="Black" Offset="0"/> 
        <GradientStop Color="White" Offset="1"/> 
       </LinearGradientBrush> 
      </Button.BorderBrush> 
      <Button.Background> 
       <ImageBrush/> 
      </Button.Background> 
     </Button> 
     <!--Build out the grid and set it to collapsed to give back the space or Hidden if you want to keep the space--> 
     <Grid Name="container" Grid.Column="0" Grid.Row="3" Height="auto" Width="auto" VerticalAlignment="Top" HorizontalAlignment="Left" Visibility="Collapsed"> 
      <Grid.ColumnDefinitions> 
       <ColumnDefinition Width="25*"/> 
       <ColumnDefinition Width="75*"/> 
      </Grid.ColumnDefinitions> 
      <Grid.RowDefinitions> 
       <RowDefinition Height="auto"/> 
       <RowDefinition Height="auto"/> 
      </Grid.RowDefinitions> 
      <Label x:Name="lblID" Content="Enter your ID: " Grid.Column="0" Grid.Row="0" Margin="5,25,45,4"/> 
      <Label x:Name="lblPW" Content="Enter your Password: " Grid.Column="0" Grid.Row="1" Margin="5,4,45,25"/> 
      <TextBox x:Name="txtbxUserID" Grid.Column="1" Grid.Row="0" Width="200" Height="25" Margin="25,25,25,4"/> 
      <PasswordBox x:Name="txtbxPW" Grid.Column="1" Grid.Row="1" Width="200" Height="25" Margin="25,4,25,25"/> 
     </Grid> 
    </Grid> 
相關問題