0

I am new to WPF and came across a situation where i think adding controls dynamically would be good.

But i don't know if there is way to it dynamically using code. I want to add following using 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>

I tried THIS method,but it did not work

4
  • @bit I tried that method but it did not work,so i posted new question Commented Apr 8, 2015 at 10:01
  • Can you state what have you tried? Commented Apr 8, 2015 at 10:02
  • The code which you claim didn't work should work. Post your code which didn't work. Commented Apr 8, 2015 at 10:04
  • 1
    learn about DataTemplates , ItemsControl , ItemsSource, Data Binding ,MVVM , you can use WPF as you use WinForms but thats not how it is meant to be. Commented Apr 8, 2015 at 10:13

1 Answer 1

0

Try below code

 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;
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks,was about to try that. But you did it faster. Thanks works perfectly
How do i set height auto or text wrapping
Remove the WrapPanel Height will internally treat as auto
There is one property called TextWrapping for TextBox, you can use it,
How do i provide name to Controls and use that name to perform function

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.