我的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
沒有好的[MCVE]認爲可靠重現問題,這是不可能說爲什麼物體移動。很顯然,你的'容器'是一種能夠讓孩子放棄孩子的方式,讓孩子們可以讓現有的孩子移動。總之,你不應該在代碼背後操縱UI。我無法提供更具體的說法,只能說您應該在XAML中聲明UI並將視圖模型(或視圖模型)中的相關數據屬性綁定到XAML對象。 –
你想如何放置控件?垂直一個接着一個? – Krishna
'TextBox'應該放在'TextBlock'的前面。 – Jazab