C# 与xaml viewmodel的接口

C# 与xaml viewmodel的接口,c#,wpf,xaml,mvvm,C#,Wpf,Xaml,Mvvm,我正在尝试创建一个简单的程序,您可以将一辆车添加到列表中,并查看make车型和年份。在我的xaml主窗口中,我有3个文本框来收集用户的信息: <TextBox Text="{Binding NewCar.Model}"/> <TextBox Text="{Binding NewCar.Make}"/> <TextBox Text="{Binding NewCar.Year}"/> 以下是我的viewmodel中的相关代码: namespace CarApp

我正在尝试创建一个简单的程序,您可以将一辆车添加到列表中,并查看make车型和年份。在我的xaml主窗口中,我有3个文本框来收集用户的信息:

<TextBox Text="{Binding NewCar.Model}"/>
<TextBox Text="{Binding NewCar.Make}"/>
<TextBox Text="{Binding NewCar.Year}"/>
以下是我的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));
        }
    }
}
}

问题似乎是我没有任何地方可以提供信息,因为我没有在viewmodel中初始化newCar对象。我必须创建一个带有空字符串的0参数构造函数,而且一切正常

为什么要传入参数?可以将文本框绑定到ViewModel的属性,然后在“添加新车”命令中,根据ViewModel中已有的信息创建新车。有很多方法可以做到这一点,但个人偏好是除非必须,否则不要在按钮命令中传递参数。祝你好运

从绑定到文本框的属性获取值,而不是从参数获取值;在初始化所有内容后,在构造函数上。
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

}
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));
        }
    }
}
}
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

}