MVVM从datagrid获取值

时间:2015-11-16 13:02:54

标签: c# wpf mvvm datagrid

我使用MVVM。

如何从DataGrid获取所选项目?

这是我的模特

Person.cs

public class Person : INotifyPropertyChanged
{
   #region Fields

   private string _firstName;
   private string _middleName;
   private string _lastName;
   private DateTime _dateOfBirth;
   private Gender _gender;

   #endregion Fields

   #region Properties

   public string FirstName
   {
      get { return _firstName; }
      set
      {
         if (value == _firstName) return;
         _firstName = value;
         nPropertyChanged();
       }
    }
    public string MiddleName
    {
       get { return _middleName; }
       set
       {
          if (value == _middleName) return;
          _middleName = value;
          OnPropertyChanged();
       }
    }
    public string LastName
    {
    get { return _lastName; }
    set
    {
       if (value == _lastName) return;
       _lastName = value;
       OnPropertyChanged();
    }
 }
 public DateTime DateOfBirth
 {
    get { return _dateOfBirth; }
    set
    {
       if (value.Equals(_dateOfBirth)) return;
       _dateOfBirth = value;
       OnPropertyChanged();
    }
 }
 public Gender Gender
 {
    get { return _gender; }
    set
    {
       if (value == _gender) return;
       _gender = value;
       OnPropertyChanged();
    }
 }

 #endregion Properties

 #region Constructors

 public Person()
 {   
 }

 public Person(string firstName, string middleName, string lastName, DateTime dateOfBirth, Gender gender)
 {
    FirstName = firstName;
    MiddleName = middleName;
    LastName = lastName;
    DateOfBirth = dateOfBirth;
    Gender = gender;
 }

 #endregion Constructors

 #region INotifyPropertyChanged

 public event PropertyChangedEventHandler PropertyChanged;

 //[NotifyPropertyChangedInvocator]
 protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
 {
    var handler = PropertyChanged;
    if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
 }

 #endregion INotifyPropertyChanged

}

   public enum Gender
   {
      Male,
      Female
   }
}

这是我的ViewModel

Ii是PersonsViewModel

namespace Learn.MVVM.Example.ViewModels
{
    public class PersonsViewModel<TViewType> : INotifyPropertyChanged, IViewModel where TViewType : IView, new()
    {
        private readonly IView _view;
        private readonly PersonModel _model;

        public ObservableCollection<Person> Persons { get; set; }
        public RelayCommand OkCommand { get; private set; }

        private string _str;

        public PersonsViewModel()
        {
            this._view = new TViewType();
            this._model = new PersonModel();
            this.Persons = new ObservableCollection<Person>(this._model.GetPersons());

            this.OkCommand = new RelayCommand(o => this.OKRun());

            _str = "Кнопка";


            this._view.SetDataContext(this);
            this._view.ShowView();


        }


        public string Str
        {
            get { return _str; }
            set
            {
                if (_str == value)
                    return;
                _str = value;
                OnPropertyChanged("Str");

            }
        }

        public ObservableCollection<Person> Observ
        {
            get { return Persons; }
            set
            {
                if (Persons == value)
                    return;
                Persons = value;
                OnPropertyChanged("Observ");


            }
        }


        public event PropertyChangedEventHandler PropertyChanged;

        //[NotifyPropertyChangedInvocator]
        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            var handler = PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
        }


        private void OKRun()
        {
            Str = "Refresh";
            //this.Persons = new ObservableCollection<Person>(this._model.SetPersons());
            this.Observ = new ObservableCollection<Person>(this._model.SetPersons());
            //OnPropertyChanged("Observ");


        }
    }
}

我如何从datagrid获取值当前行? 我如何从datagrid获取索引当前行?

1 个答案:

答案 0 :(得分:3)

根据我的经验,实现这一目标的一个很好的方法是将SelectedItem绑定到视图模型中的属性:

public Person SelectedPerson { get; set; }

您的DataGrid将如下所示:

<DataGrid SelectedItem="{Binding SelectedPerson}" ... >