与xaml视图模型接口

本文关键字:接口 模型 视图 xaml | 更新日期: 2023-09-27 17:53:20

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

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

然后用户点击"添加"按钮,该车应该被添加到列表中:

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

我已经验证了触摸命令可以正确地添加汽车,但是,似乎潜在的问题是文本框没有将输入的内容绑定到NewCar对象的各自属性,因为当我单击添加按钮时,参数仍然为空。

下面是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");
        }
    }

,这里是我的视图模型的相关代码:

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

与xaml视图模型接口

为什么要传递一个参数呢?您可以将TextBoxes绑定到ViewModel的属性,然后在add new car命令中,根据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
}

似乎问题是,我没有任何地方的信息去,因为我没有一个newCar对象初始化的视图模型。我必须用空字符串创建一个0参数构造函数,一切都正常工作!