2013-12-23 161 views
0

當我從頂部設置Margin15TextBoxStackPanel的保證金問題

x.Margin = new Thickness(100, 15, 0, 0); 

這工作得很好,一切就OK了,但後來我想打一個ComboBox也出現從15px頂部 - 它不起作用。

y.Margin = new Thickness(0, 15, 0, 0); 

這是按鈕的代碼我點擊創建組合框和文本框:

int t = 0; 
private void btnAddTitle_Click(object sender, RoutedEventArgs e) 
{ 
     TextBox x = new TextBox(); 
     x.Name = "Title" + t; 
     x.Text = "Title..."; 
     x.FontWeight = FontWeights.Bold; 
     x.FontStyle = FontStyles.Italic; 
     x.TextWrapping = TextWrapping.Wrap; 
     x.Height = 25; 
     x.Width = 200; 
     x.HorizontalAlignment = System.Windows.HorizontalAlignment.Left; 
     x.Margin = new Thickness(100, 15, 0, 0); 
     spStandard.Children.Add(x); 


     ComboBox y = new ComboBox(); 
     y.Name = "Combo" + t; 
     y.Text = (t + 1).ToString(); 
     y.Height = 25; 
     y.Width = 45; 
     y.HorizontalAlignment = System.Windows.HorizontalAlignment.Left; 
     y.Margin = new Thickness(0, 15, 0, 0); 
     spStandard.Children.Add(y); 

     t++; 
} 

這裏是當我運行該應用程序會發生什麼了一張圖片 - 它顯示了組合框變放:

Controls displayed by my program

+1

'TextBox'也有「margin left」設置爲100,期望的結果是什麼? –

+0

@NovitchiS我希望組合框出現在左側的TextBox旁邊,但它不會將它放在同一行上。 MarginLeft只是將文本框移動到左邊,以便我可以適合ComboBox。 –

+0

我不明白這個問題,你不想讓它們垂直對齊嗎? –

回答

0

這裏是你的問題的解決方案,你不應該在代碼中創建控件後面,但它會完成這項工作:

int t = 0; 
private void btnAddTitle_Click(object sender, RoutedEventArgs e) 
{ 
    //a new stackpanel is used to arrange items Horizontally for each line 
    StackPanel sp = new StackPanel() { Orientation = Orientation.Horizontal }; 

    TextBox x = new TextBox(); 
    x.Name = "Title" + t; 
    x.Text = "Title..."; 
    x.FontWeight = FontWeights.Bold; 
    x.FontStyle = FontStyles.Italic; 
    x.TextWrapping = TextWrapping.Wrap; 
    x.Height = 25; 
    x.Width = 200; 
    x.Margin = new Thickness(0, 15, 0, 0); 

    ComboBox y = new ComboBox(); 
    y.Name = "Combo" + t; 
    y.Text = (t + 1).ToString(); 
    y.Height = 25; 
    y.Width = 45; 
    y.Margin = new Thickness(0, 15, 0, 0); 

    sp.Children.Add(y); 
    sp.Children.Add(x);   

    spStandard.Children.Add(sp); 

    t++; 
} 
+0

'{Orientation = Horizo​​ntal};'位不存在。 –

+0

查看編輯過的帖子。 –

+0

這不起作用,它只是做同樣的事情。 –