StackPanel保证金问题

时间:2013-12-23 12:38:31

标签: c# wpf combobox

当我从 Top Margin的{​​{1}}设置15时:

TextBox

这很好,一切都还可以,但是我想让x.Margin = new Thickness(100, 15, 0, 0); 从顶部出现ComboBox - 它不起作用。

15px

这是我单击以创建ComboBox和TextBox的按钮的代码:

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

这是我运行应用程序时会发生什么的图片 - 它显示了ComboBox的放置位置:

Controls displayed by my program

1 个答案:

答案 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++;
}