c# - 绑定(bind)在 WPF MVVM C#​​ 中不起作用

标签 c# wpf mvvm

我正在研究 WPF C#、MVVM 模型。我对 View 中的 Save_Button 有疑问。 getter、setter、RelayCommand 初始化等所有东西都在工作,当我点击“保存”按钮时什么也没有发生。所以看起来从 View 到 ViewModel 的绑定(bind)不起作用。我在这里只提供 View 、 View 模型和命令部分的必要文件。请帮忙。

VehicalForm.xaml

<Window x:Class="Seris.VehicalForm"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<WrapPanel Orientation="Vertical" Margin="10 " >
    <Label Content="Vehical No" HorizontalAlignment="Left"/>
    <TextBox Name="VehicalNo_Text" Height="23" TextWrapping="Wrap" Text="TextBox"  HorizontalAlignment="Left"/>
    <Label Content="Model" HorizontalAlignment="Left"/>
    <TextBox Name="Model_Text" Height="23" TextWrapping="Wrap" Text="TextBox" HorizontalAlignment="Left" />
    <Label Content="Manufacturing Date" HorizontalAlignment="Left"/>
    <DatePicker/>
    <Label Content="IU No" HorizontalAlignment="Left"/>
    <TextBox Height="23" Name="IUNO_Text" TextWrapping="Wrap" Text="TextBox" HorizontalAlignment="Left"/>
    <Label Content="Personnel" HorizontalAlignment="Left"/>
    <ComboBox Name="Personnel_Combo" HorizontalAlignment="Left" Width="116"/>
    <Separator Height="20" RenderTransformOrigin="0.5,0.5" Width="16"/>
    <Button Name="Save_Button" Command="{Binding SaveToList}" Content="Save" Width="66"/>
    <ListView Height="294" Width="371" >
        <ListView.View>
            <GridView>
                <GridViewColumn Header="lkj"/>
                <GridViewColumn Header="lkj"/>
                <GridViewColumn Header="lkj"/>
            </GridView>
        </ListView.View>
    </ListView>
</WrapPanel>

VehicalForm.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Seris
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class VehicalForm : Window
{
    public VehicalForm()
    {
        InitializeComponent();
    }


}
}

VehicalMainViewModel.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Seris.Models;
using System.Collections.ObjectModel;
using System.Windows.Input;
using Seris.Commands;
using Seris.ViewModels;

namespace Seris.ViewModels
{
public class VehicalMainViewModel : ObservableObject
{
ObservableCollection<VehicalModel> listItems = new ObservableCollection<VehicalModel>();

    #region Getter-Setter
    private string _VehicalNo;

    public string VehicalNo 
    {
        get { return _VehicalNo; }
        set
        {
            if (value != _VehicalNo)
            {
                _VehicalNo = value.Trim();
                OnPropertyChanged("ProductName");
            }
        }
    }
    private string _Model;

    public string Model
    {
        get { return _Model; }
        set
        {
            if (value != _Model)
            {
                _Model = value.Trim();
                OnPropertyChanged("ProductName");
            }
        }
    }
    private DateTime _ManufacturingDate;

    public DateTime ManufacturingDate
    {
        get { return _ManufacturingDate; }
        set
        {
            if (value != _ManufacturingDate)
            {
                _ManufacturingDate = value;
                OnPropertyChanged("ProductName");
            }
        }
    }
    private string _IUNo;

    public string IUNo
    {
        get { return _IUNo; }
        set
        {
            if (value != _IUNo)
            {
                _IUNo = value.Trim();
                OnPropertyChanged("ProductName");
            }
        }
    }
    private string _PersonnelName;

    public string PersonnelName
    {
        get { return _PersonnelName; }
        set
        {
            if (value != _PersonnelName)
            {
                _PersonnelName = value.Trim();
                OnPropertyChanged("ProductName");
            }
        }
    } 
    #endregion

    private ICommand _saveButton_Command;

    public ICommand SaveButton_Command
    {
        get { return _saveButton_Command; }
        set { _saveButton_Command = value; }
    }

    public void SaveToList(object o1)
    {
        listItems.Add(new VehicalModel(VehicalNo,Model,ManufacturingDate,IUNo,PersonnelName));
    }
    public void RemoveFromList()
    {

    }
    public VehicalMainViewModel()
    {
        VehicalModel vm=new VehicalModel();
        SaveButton_Command = new RelayCommand(new Action<object>(SaveToList));
    }
}
}

RelayCommand.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;

namespace Seris.Commands
{
public class RelayCommand : ICommand
{
    private Action<object> _action;
    public RelayCommand(Action<object> action)
    {
        _action = action;
    }
    public bool CanExecute(object parameter)
    {
        return true;
    }
    public event EventHandler CanExecuteChanged;
    public void Execute(object parameter)
    {
        _action(parameter);
    }

}
}

最佳答案

在 XAML 中尝试

<Button Name="Save_Button" Command="{Binding SaveButton_Command}" … />

关于c# - 绑定(bind)在 WPF MVVM C#​​ 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25073545/

相关文章:

android - 对象应存储在mvvm中的位置

silverlight - 使用自定义构造函数加载silverlight控件

c# - Thread.Abort() 在工作线程调用 Dispatcher.Invoke() 时创建死锁

c# - 我应该如何跟踪使用中的 Objs,C#

c# - 使用命名空间作为类型错误

c# - 如何让 WPF ContentControl 内容拉伸(stretch)?

c# - 如何在 WPF 中检索 DataGrid 内容的总高度?

java - 如何在ZK MVVM中使用javascript调用java方法?

c# - 单元测试基于 umbraco 的网站

c# - ICommand接口(interface)的高效使用