Reputation: 279
Im trying to create a simple program where you can add a car to a list and view the make model and year. In my xaml mainwindow I have 3 text boxes to collect the information from the user:
<TextBox Text="{Binding NewCar.Model}"/>
<TextBox Text="{Binding NewCar.Make}"/>
<TextBox Text="{Binding NewCar.Year}"/>
Then the user clicks the "add" button and the car should be added to the list:
<Button Content="Add" Command="{Binding TouchCommand}" CommandParameter="{Binding NewCar}"/>
I've verified that the touch command work properly adding cars but, it seems the underlying issue is that the text boxes are not binding what is entered into them to the respective attributes of the NewCar object because when I click the add button the parameter is still null.
Here is the execute method of add:
public void Execute(object parameter)
{
Car param = parameter as Car;
if (param != null)
{
lib.List.Add(param);
Console.WriteLine("{0} {1} {2}", param.Make, param.Model, param.Year);
}
else
{
Console.WriteLine("Param is null");
}
}
and here is the relevant code from my viewmodel:
namespace CarApp
{
public class Carvm : INotifyPropertyChanged
{
private CarLib carLibrary;
private Car currentCar;
private DeleteCommand rmCommand;
private AddCommand touchCommand;
private Car newCar;
public Car NewCar
{
get { return newCar; }
set
{
newCar = value;
NotifyPropertyChanged("NewCar");
}
}
public Car CurrentCar
{
get { return currentCar; }
set
{
currentCar = value;
NotifyPropertyChanged("CurrentCar");
}
}
public CarLib CarLibrary
{
get { return carLibrary; }
set
{
carLibrary = value;
NotifyPropertyChanged("CarLibrary");
}
}
public DeleteCommand RMCommand
{
get { return rmCommand; }
set { rmCommand = value; }
}
public AddCommand TouchCommand
{
get { return touchCommand; }
set { touchCommand = value; }
}
public Carvm()
{
carLibrary = new CarLib();
carLibrary.List.Add(new Car("chevy", "corvette", "2016"));
carLibrary.List.Add(new Car("ford", "gt", "2016"));
carLibrary.List.Add(new Car("bmw", "m3", "2005"));
rmCommand = new DeleteCommand(carLibrary);
touchCommand = new AddCommand(carLibrary);
}
public event PropertyChangedEventHandler PropertyChanged;
protected void NotifyPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
}
}
Upvotes: 3
Views: 82
Reputation: 414
public Carvm()
{
carLibrary = new CarLib();
carLibrary.List.Add(new Car("chevy", "corvette", "2016"));
carLibrary.List.Add(new Car("ford", "gt", "2016"));
carLibrary.List.Add(new Car("bmw", "m3", "2005"));
rmCommand = new DeleteCommand(carLibrary);
touchCommand = new AddCommand(carLibrary);
NewCar = new Car(); // this line is added
}
Upvotes: 1
Reputation: 225
Why pass in a parameter at all? You could bind the TextBoxes to properties of the ViewModel and then, in the add new car command, create a new car based on the information already in the ViewModel. Lots of ways to do this, but personal preference would be to not pass a parameter in button command unless you have to. Good luck!
Upvotes: 2
Reputation: 279
Seems like the issue was that I didn't have anywhere for the information to go because I didn't have a newCar object initialized in the viewmodel. I had to create a 0 parameter constructor with null strings and everything worked properly!
Upvotes: 0