使用XAML绑定对象列表

我有一个简单属性的模型类

class ManagementFunctionModel { #region members int _RangeLeft; int _RangeTop; int _RangeRight; int _RangeBottom; #endregion #region properties public int RangeLeft { get { return _RangeLeft; } set { _RangeLeft = value; } } public int RangeTop { get { return _RangeTop; } set { _RangeTop = value; } } public int RangeRight { get { return _RangeRight; } set { _RangeRight = value; } } public int RangeBottom { get { return _RangeBottom; } set { _RangeBottom = value; } } #endregion } 

我的模型视图类如下。 我还没有定义所有属性(仍然是TODO)

 class ManagementFunctionModelView : INotifyPropertyChanged { #region Members ManagementFunctionModel _MngModel; #endregion #region Construction /// Constructs the default instance of a ManagementFunctionModelView public ManagementFunctionModelView() { } #endregion #region Properties public ManagementFunctionModel MngModel { get { return _MngModel; } set { _MngModel = value; } } public int RangeLeft { get { return MngModel.RangeLeft; } set { if (MngModel.RangeLeft != value) { MngModel.RangeLeft = value; RaisePropertyChanged("RangeLeft"); } } } #endregion #region INotifyPropertyChange public event PropertyChangedEventHandler PropertyChanged; #endregion #region Methods private void RaisePropertyChanged(string propertyName) { // take a copy to prevent thread issues PropertyChangedEventHandler handler = PropertyChanged; if (handler != null) { handler(this, new PropertyChangedEventArgs(propertyName)); } } #endregion } 

我想创建一个ManagementFunctionModel列表并以XAMLforms显示。 用户应该能够从用户界面添加和删除到列表中。

如何在XAML中使用“绑定”? 我应该在XAML中使用什么控件来显示和编辑/删除所选记录等网格?

  ViewModel -> VMStudent.cs using GalaSoft.MvvmLight; using GalaSoft.MvvmLight.Command; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.ComponentModel; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; using System.Windows; using WpfDesktopCrud.Model; namespace WpfDesktopCrud.ViewModel { class VMStudent : ViewModelBase, IDataErrorInfo { public VMStudent() { try { Students = new ObservableCollection(); Students.Add(new Student { id = 1, name = "Dhru", mobno = "0000000000", address = "Shahibaug", email = "dhrusoni84@gmail.com" }); Students.Add(new Student { id = 2, name = "Soni", mobno = "9033259059", address = "Vastrapur", email = "dhru_soni@yahoo.com" }); SaveCommand = new RelayCommand(Save); NewCommand = new RelayCommand(New); DeleteCommand = new RelayCommand(Delete); PropertyChanged += VMStudent_PropertyChanged; } catch (Exception ex) { throw; } } void VMStudent_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e) { switch (e.PropertyName) { case "Selected": if (Selected != null) { ID = Selected.id; Name = Selected.name; MobileNo = Selected.mobno; Address = Selected.address; Email = Selected.email; } break; } } #region Properties private ObservableCollection _Students; public ObservableCollection Students { get { return _Students; } set { _Students = value; RaisePropertyChanged("Students"); } } private Student _Selected; public Student Selected { get { return _Selected; } set { _Selected = value; RaisePropertyChanged("Selected"); } } private int _ID; public int ID { get { return _ID; } set { _ID = value; RaisePropertyChanged("ID"); } } private string _Name; public string Name { get { return _Name; } set { _Name = value; RaisePropertyChanged("Name"); } } private string _MobileNo; public string MobileNo { get { return _MobileNo; } set { _MobileNo = value; RaisePropertyChanged("MobileNo"); } } private string _Address; public string Address { get { return _Address; } set { _Address = value; RaisePropertyChanged("Address"); } } private string _Email; public string Email { get { return _Email; } set { _Email = value; RaisePropertyChanged("Email"); } } #endregion #region Commands public RelayCommand SaveCommand { get; set; } public RelayCommand NewCommand { get; set; } public RelayCommand DeleteCommand { get; set; } #endregion #region Methods public void Save() { if (Selected != null) { if (Selected.id != 0) { Selected.name = this.Name; Selected.mobno = this.MobileNo; Selected.address = this.Address; Selected.email = this.Email; MessageBox.Show("Record Updated.", "WPF MVVM", MessageBoxButton.OK, MessageBoxImage.Information, MessageBoxResult.OK); this.ID = 0; this.Name = string.Empty; this.Address = string.Empty; this.MobileNo = string.Empty; this.Email = string.Empty; } } else { try { Students.Add(new Student { id = Students.Count + 1, name = this.Name, mobno = this.MobileNo, address = this.Address, email = this.Email }); MessageBox.Show("Record Saved.", "WPF MVVM", MessageBoxButton.OK, MessageBoxImage.Information, MessageBoxResult.OK); this.ID = 0; this.Name = string.Empty; this.MobileNo = string.Empty; this.Address = string.Empty; this.Email = string.Empty; } catch (Exception ex) { throw; } } } public void New() { if (Selected != null) { this.ID = 0; this.Name = string.Empty; this.MobileNo = string.Empty; this.Address = string.Empty; this.Email = string.Empty; Selected.id = 0; } Selected = null; } public void Delete() { try { Students.Remove(Selected); this.Name = string.Empty; this.MobileNo = string.Empty; this.Address = string.Empty; this.Email = string.Empty; MessageBox.Show("Record Deleted.", "WPF MVVM", MessageBoxButton.OK, MessageBoxImage.Information, MessageBoxResult.OK); } catch (Exception ex) { throw; } } #endregion public string Error { get { throw new NotImplementedException(); } } public string this[string columnName] { get { string result = null; if (columnName == "Name") { if (string.IsNullOrEmpty(Name)) { result = "Name Cannot Be Empty"; return result; } string st = @"!|@|#|\$|%|\?|\>|\<|\*"; if (Regex.IsMatch(Name, st)) { result = "special chatachter are not allowed"; return result; } } if (columnName == "MobileNo") { if (string.IsNullOrEmpty(MobileNo)) { result = "Mobile Can not be empty"; return result; } if (MobileNo.Length > 10 || MobileNo.Length < 10) { result = "number should be 10 charachters"; return result; } string s1 = @"^[0-9]*$"; if (!Regex.IsMatch(MobileNo, s1)) { result = "Mobile number contain numeric value "; return result; } else { return null; } } if (columnName == "Address") { if (string.IsNullOrEmpty(Address)) { result = "fill your address"; return result; } } if (columnName == "Email") { string s1 = @"^[a-zA-Z][\w\.-]{2,28}[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$"; if (string.IsNullOrEmpty(Email)) { result = "Email can not be empty"; return result; } if (Regex.IsMatch(Email, s1)) //|| Regex.IsMatch(Email, s2)) { return null; } else { result = "Not valid email(format: dhru@gmail.com)"; return result; } } return null; } } } } View - > Student.xaml                                                        Model -> Student.cs using GalaSoft.MvvmLight; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace WpfDesktopCrud.Model { class Student : ViewModelBase { private int _id; public int id { get { return _id; } set { _id = value; RaisePropertyChanged("id"); } } private string _name; public string name { get { return _name; } set { _name = value; RaisePropertyChanged("name"); } } private string _mobno; public string mobno { get { return _mobno; } set { _mobno = value; RaisePropertyChanged("mobno"); } } private string _address; public string address { get { return _address; } set { _address = value; RaisePropertyChanged("address"); } } private string _email; public string email { get { return _email; } set { _email = value; RaisePropertyChanged("email"); } } private DateTime _timer; public DateTime Timer { get { return _timer; } set { _timer = value; RaisePropertyChanged("timer"); } } } }