Beispiel #1
0
 public override void OpenSession()
 {
     _DictionaryService.StartSession();
 }
Beispiel #2
0
        public RealtorAgencyDictionaryViewModel(IServiceLocator serviceLocator, IRealtorAgencyService dictionaryService,
                                                IMessageService messageService)
            : base(serviceLocator, messageService)
        {
            _DictionaryService = dictionaryService;
            UpdateState(State.View);
            PropertyChanged += (sender, args) =>
            {
                if (args.PropertyName == PropertySupport.ExtractPropertyName(() => Name) ||
                    args.PropertyName == PropertySupport.ExtractPropertyName(() => Director) ||
                    args.PropertyName == PropertySupport.ExtractPropertyName(() => Address) ||
                    args.PropertyName == PropertySupport.ExtractPropertyName(() => Contacts) ||
                    args.PropertyName == PropertySupport.ExtractPropertyName(() => Description))
                {
                    OkCommand.RaiseCanExecuteChanged();
                    CancelCommand.RaiseCanExecuteChanged();
                }

                if (args.PropertyName == PropertySupport.ExtractPropertyName(() => SelectedRealtorAgency))
                {
                    UpdateSelectedRealtorAgency();
                }
            };
            _DictionaryService.StartSession();

            AddCommand = new DelegateCommand(() =>
            {
                ClearProperties();
                UpdateState(State.Add);
                ButtonUpdate();
            }, CanAdd);

            ChangeCommand = new DelegateCommand(() =>
            {
                UpdateState(State.Edit);
                ButtonUpdate();
            }, CanEdit);

            OkCommand = new DelegateCommand(() =>
            {
                if (StateEnum == State.Add)
                {
                    var viewModel = CreateNewViewModel(CreateNewModel());
                    var error     = viewModel.Error;
                    if (error == null)
                    {
                        viewModel.SaveToDatabase();
                        _Entities.Add(viewModel);
                        ClearProperties();
                        UpdateState(State.View);
                    }
                    else
                    {
                        _MessageService.ShowMessage(error, "Ошибка", image: MessageBoxImage.Error);
                    }
                }

                if (StateEnum == State.Edit)
                {
                    SelectedRealtorAgency.Name        = Name;
                    SelectedRealtorAgency.Address     = Address;
                    SelectedRealtorAgency.Contacts    = Contacts;
                    SelectedRealtorAgency.Description = Description;
                    SelectedRealtorAgency.Director    = Director;
                    var error = SelectedRealtorAgency.Error;

                    if (error == null)
                    {
                        SelectedRealtorAgency.UpdateModelFromValues();
                        SelectedRealtorAgency.SaveToDatabase();
                        UpdateState(State.View);
                    }
                    else
                    {
                        SelectedRealtorAgency.UpdateValuesFromModel();
                        _MessageService.ShowMessage(error, "Ошибка", image: MessageBoxImage.Error);
                    }
                }

                ButtonUpdate();
            }, CanOk);

            CancelCommand = new DelegateCommand(() =>
            {
                if (StateEnum == State.Add)
                {
                    ClearProperties();
                }

                if (StateEnum == State.Edit)
                {
                    UpdateSelectedRealtorAgency();
                }

                UpdateState(State.View);
                ButtonUpdate();
            }, CanCancel);

            SelectAndChangeCommand = new DelegateCommand(() =>
            {
                UpdateSelectedRealtorAgency();
                UpdateState(State.Edit);
                ButtonUpdate();
            }, CanSelectAndChange);
        }