c# - 与 xaml View 模型交互

标签 c# wpf xaml mvvm

我正在尝试创建一个简单的程序,您可以在其中将汽车添加到列表中并查看品牌型号和年份。在我的 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}"/>

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

这里是add的execute方法:

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

这是我的 View 模型中的相关代码:

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

最佳答案

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

关于c# - 与 xaml View 模型交互,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30652829/

相关文章:

c# - 解决方案第一次构建 w/missing auto-generated .g.cs 文件失败,第二次构建成功

wpf - 可以使用BackgroundWorker的ReportProgress来做其他事情吗?

C# MSScriptControl 将类传递给函数

c# - 将 excel 工作表保存到我在本地计算机上创建的特定文件夹,并将此 excel 工作表设置为只读

c# - URL 中的 ~ 是什么意思

c# - 如何让任务不在 UI 线程上执行

c# - 如何在 Page.Resources 中包含两个资源?

c# - WPF 4.0 的日期时间选择器

wpf - 无法将 DependencyProperty 绑定(bind)到我的 UserControl

c# - Android Azure 删除所有行