首页 文章

使用代码动态添加WPF控件[复制]

提问于
浏览
0

这个问题在这里已有答案:

我是WPF的新手,遇到了一种情况,我认为动态添加控件会很好 .

但我不知道是否有 is way to it dynamically using code . 我想使用Code添加以下内容

<WrapPanel Height="39">
      <TextBox Width="93" Margin="-1,5,0,0"></TextBox>
      <TextBox Width="76" Margin="0,5,0,0"></TextBox>
      <TextBox Width="70" Margin="0,5,0,0"></TextBox>
      <TextBox Width="70" Margin="0,5,0,0"></TextBox>
      <TextBox Width="127" Margin="0,5,0,0"></TextBox>
      <TextBox Width="100" Margin="0,5,0,0"/>
</WrapPanel>

我尝试了THIS方法,但它没有用

1 回答

  • 0

    试试下面的代码

    var panel = new WrapPanel() { Height = 39 };
                panel.Children.Add(new TextBox() { Width = 93, Margin = new Thickness(-1, 5, 0, 0) });
                panel.Children.Add(new TextBox() { Width = 76, Margin = new Thickness(0, 5, 0, 0) });
                panel.Children.Add(new TextBox() { Width = 70, Margin = new Thickness(0, 5, 0, 0) });
                panel.Children.Add(new TextBox() { Width = 70, Margin = new Thickness(0, 5, 0, 0) });
                panel.Children.Add(new TextBox() { Width = 127, Margin = new Thickness(0, 5, 0, 0) });
                panel.Children.Add(new TextBox() { Width = 100, Margin = new Thickness(0, 5, 0, 0) });
                this.Content = panel;
    

相关问题