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.