0

I have stack of TextBox.I want to add new TextBox at the end of stack on each button click my code is:

<TextBox Height="25" Width="100" Margin="15,15,0,15" > </TextBox>
 <Button Content="Add Metric" Width="100" HorizontalAlignment="Center" Height="30" Grid.Row="1" Grid.Column="0"/>

I am using MVVM.help me on this with the example code.how to achieve this?

4
  • 1
    You want to generate using code behind? Commented Jun 10, 2014 at 8:40
  • This is not how it works. You have to try something first. Commented Jun 10, 2014 at 8:43
  • what will you with the textboxes? will there be any data backing those textboxes? Commented Jun 10, 2014 at 8:43
  • You should use data binding and an ItemsControl. Commented Jun 10, 2014 at 8:47

3 Answers 3

1

Your XAML file should look like this (add "button_Click" event handler to the button):

<StackPanel x:Name="tbPanel">
...
</StackPanel>

<Button Content="Add Metric" Width="100" HorizontalAlignment="Center" Height="30" Grid.Row="1" Grid.Column="0" Click="button_Click"/>

And in your code behind file add method

private void button1_Click(object sender, RoutedEventArgs e)
{
  var newTextBox = new TextBox();
  // here set new textbox parameters
  tbPanel.Children.Add(newTextBox);
}
Sign up to request clarification or add additional context in comments.

1 Comment

While this answers the question, it most likely does not solve OP's problem. OP claims to use MVVM, so data binding and an ItemsControl should be used.
0

I would put the textboxes in a StackPanelPanel(container) and on the buttons click-event build up a new textbox and add it add the end of the panel.

Pseudo:

var textBox = new TextBox();
textBox.Width = 100;
...

this.ThePanel.Controls.Add(textBox);

If code-behind then I would recommend adding all controls from code-behind and store them in a array of some sort (maybe Queue or List) so you easely can get the values from them later.

1 Comment

While this answers the question, it most likely does not solve OP's problem. OP claims to use MVVM, so data binding and an ItemsControl should be used.
0

Here is what I do when writing MVVM application.

Create class that will handle your commands (this is just a quick sample):

class CommandHandler:ICommand
{
    private Action action;
    private Func<bool> isEnabled;

    public CommandHandler(Action _action, Func<bool> _isEnabled)
    {
        action = _action;
        isEnabled = _isEnabled;
    }

    public bool CanExecute(object parameter)
    {
        return isEnabled();
    }

    public event EventHandler CanExecuteChanged;

    public void Execute(object parameter)
    {
        action();
    }
}

Now under you ViewModel, you have to create tow things: 1) Create method that you will call with your button click like the following

Public void ButtonWasClicked()
{
   //do something here
}

2) Create actual command

    private ICommand _cmd_MyCommand;
    public ICommand cmd_MyCommand
    {
        get { return _cmd_MyCommand ?? new CommandHandler(ButtonWasClicked, new Func<bool>(() => true)); }
    }

and finally under your View (WPF window) you can bind the command like this:

<Button  Command="{Binding cmd_MyCommand}">

Obviously, there are also other methods how you can use with Commands in WPF (e.g. some folks might suggest RelayCommand). This may seem a bit strange in the beginning (why so much trouble for a simple button click), but you will get into it really quickly once you start using them.

Here is some nice video on MVVM commands that I used in the past as well: http://www.youtube.com/watch?v=EpGvqVtSYjs

Good luck!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.