Делаю CRUD на WPF MVVM. Контрол RadGridView но это в принципе не важно. С командами разобрался. Застрял с валидацией..
Это моя viewModel
public class DirectoryViewModel : TurnikamBaseModel
{
private TurnikamEntities _dataContext;
private ObservableCollection<TBL_SEX> tbl_sex;
public ObservableCollection<TBL_SEX> Tbl_sex
{
get { return tbl_sex; }
set { tbl_sex = value; }
}
private TBL_SEX selectedSex;
public TBL_SEX SelectedSex
{
get { return selectedSex; }
set
{
if (this.selectedSex != value)
{
selectedSex = value;
((RelayCommand)this.SexDeleteCommand).IsEnabled = true;
this.OnPropertyChanged(() => this.selectedSex);
}
}
}
public DirectoryViewModel()
{
_dataContext = new TurnikamEntities();
tbl_sex = new ObservableCollection<TBL_SEX>(_dataContext.TBL_SEX);
sexSave = new RelayCommand(SexSave) { IsEnabled = true };
sexAdd = RadGridViewCommands.BeginInsert;
sexDelete = new RelayCommand(SexDelete) { IsEnabled = false };
}
ICommand sexSave;
ICommand sexAdd;
ICommand sexDelete;
public ICommand SexSaveCommand
{
get { return sexSave; }
set { sexSave = value; }
}
private void SexSave()
{
_dataContext.SaveChanges();
}
public ICommand SexAddCommand
{
get { return sexAdd; }
}
public ICommand SexDeleteCommand
{
get { return sexDelete; }
}
private void SexDelete()
{
if (SelectedSex != null)
{
if (SelectedSex.SexGUID!=Guid.Empty)
{
this._dataContext.TBL_SEX.DeleteObject(SelectedSex);
}
Tbl_sex.Remove(SelectedSex);
OnPropertyChanged("Tbl_sex");
}
else
{
((RelayCommand)this.SexDeleteCommand).IsEnabled = false;
}
}