Interacting with the xaml viewmodel

I am trying to create a simple program where you can add a car to the list and see the model model and year. In my main xaml window, I have 3 text boxes to collect information from the user:

<TextBox Text="{Binding NewCar.Model}"/>
<TextBox Text="{Binding NewCar.Make}"/>
<TextBox Text="{Binding NewCar.Year}"/>

      

Then the user clicks on the "add" button and the car should be added to the list:

<Button Content="Add" Command="{Binding TouchCommand}" CommandParameter="{Binding NewCar}"/>

      

I have verified that the touch command works correctly with adding cars, but the main problem seems to be that the text boxes are optional, what is entered in them corresponding to the attributes of the NewCar object, because when I click the add button the parameter is still zero.

Here's a way to do 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));
        }
    }
}
}

      

+3


source to share


3 answers


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

}

      



+1


source


Why pass a parameter at all? You can bind TextBoxes to the properties of the ViewModel, and then, in the command to add a new car, create a new car based on the information already in the ViewModel. There are many ways to do this, but personal preference is not to pass a parameter in a button command unless you need to. Good luck!



+2


source


It looks like the problem was that I was unable to find the information because I didn't have the newCar object initialized in the viewmodel. I had to create a 0 parameter constructor with null strings and everything worked correctly!

0


source







All Articles