How to use the textbox value in mvvm commands

advertisements

I'm still figuring out mvvm and I want to make my app the right way by splitting up model viewmodel and view. In the past I always took the value of the textbox in the view directly by using mytextbox.Text.

I created a command in the viewmodel to add a new person to the network. but I can't get the values of the textboxes into the command in the viewmodel.

This is the code I have so far in model

public class Person : INotifyPropertyChanged
{
    public Person()
    { }

    public Person(String FirstName)
    {
        this._firstName = FirstName;
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private string _firstName;

    public string FirstName    // the Name property
    {
        get { return this._firstName; }
        set { this._firstName = value; NotifyPropertyChanged("FirstName"); }
    }
}

in the viewmodel I have

public class NetworkViewModel: INotifyPropertyChanged
{

    private ObservableCollection<Person> _networkList1 = new ObservableCollection<Person>();
    public ObservableCollection<Person> NetworkList1 //Binds with the listbox
    {
        get { return _networkList1; }
        set { _networkList1 = value; RaisePropertyChanged("NetworkList1"); }
    }

     public NetworkViewModel()
    {
        AddPersonCommand = new RelayCommand(AddPerson);
    }

    private ICommand _addPersonCommand;
    public ICommand AddPersonCommand // Binding with view
    {
        get { return _addPersonCommand; }
        set { _addPersonCommand = value; }
    }

   public void AddPerson(object obj)
    {

    if(cb_group.Text.ToUpper() == "PRIMARY")
    {
        _networkList1.Add(new Person(){ FirstName = tb_firstName.Text,});
    }
}

in XAML

 <TextBox x:Name="tb_firstName"  Text="{Binding Path=FirstName}"/>

<Button x:Name="btn_add" Command="{Binding AddPersonCommand }"/>

What I would like is to make the value of tb_firstname and cb_group to be used in the viewmodel so I can make the command work. Thanks for all your help. I'm just learning as I go.


ok so in your textbox you currently have:

<TextBox x:Name="tb_firstName"  Text="{Binding Path=FirstName}"/>

What this means is that it is looking for a property on your viewmodel called FirstName, so you will need a add a string property to your viewmodel for it. Then when you set the FirstName on the new Person, you simply set it to the property.

new Person() { FirstName = this.FirstName };

For binding a combobox, it will need an itemsSource - this is the collection of items, in your case the NetworkList1 to be shown. As its binding to a list of objects you will also need to set the DisplayMemberPath - this is the name of the property you want to be shown in the combo. You will also need to bind the SelectedItem property and create a property in your viewmodel to store it. This is how you know which item in your list is currently selected.

<Combobox ItemsSource={Binding Path=NetworkList1, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}
SelectedItem = {Binding Path= SelectedPerson, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}
DisplayMemberPath=FirstName/>

viewModel:

public Person SelectedPerson
{
  //put in get/set/propertychanged
}

public string FirstName
{
   //put in get/set/propertychanged
}