Newer
Older
Correlator / Correlator / ViewModels / SoundSpeedDialogViewModel.cs
using System;
using System.Collections.ObjectModel;
using System.Windows.Controls;
using Correlator.DataService;
using Correlator.Model;
using Correlator.Util;
using Prism.Commands;
using Prism.Mvvm;
using Prism.Services.Dialogs;

namespace Correlator.ViewModels
{
    public class SoundSpeedDialogViewModel : BindableBase, IDialogAware
    {
        private readonly IApplicationDataService _dataService;
        public event Action<IDialogResult> RequestClose;
        private MaterialVelocity _materialVelocity;

        #region VM

        public string Title { get; private set; } = string.Empty;
        private string _materialName;

        public string MaterialName
        {
            get => _materialName;
            private set
            {
                _materialName = value;
                RaisePropertyChanged();
            }
        }

        private ObservableCollection<MaterialVelocity> _velocityCollection;

        public ObservableCollection<MaterialVelocity> VelocityCollection
        {
            get => _velocityCollection;
            private set
            {
                _velocityCollection = value;
                RaisePropertyChanged();
            }
        }

        #endregion

        #region DelegateCommand

        public DelegateCommand GoBackCommand { get; set; }
        public DelegateCommand DeleteSpeedCommand { get; set; }
        public DelegateCommand AddSpeedCommand { get; set; }
        public DelegateCommand EditSpeedCommand { get; set; }
        public DelegateCommand<ListView> ItemSelectedCommand { get; set; }

        #endregion

        public SoundSpeedDialogViewModel(IApplicationDataService dataService, IDialogService dialogService)
        {
            _dataService = dataService;

            GoBackCommand = new DelegateCommand(delegate
            {
                RequestClose?.Invoke(new DialogResult(ButtonResult.Cancel));
            });

            ItemSelectedCommand = new DelegateCommand<ListView>(delegate(ListView view)
            {
                _materialVelocity = (MaterialVelocity)view.SelectedItem;
            });

            DeleteSpeedCommand = new DelegateCommand(delegate
            {
                if (_materialVelocity == null)
                {
                    dialogService.ShowDialog(
                        "AlertMessageDialog",
                        new DialogParameters
                        {
                            { "AlertType", AlertType.Error }, { "Title", "温馨提示" }, { "Message", "未选择任何项,无法删除" }
                        },
                        delegate { }
                    );
                    // MessageBox.Show("未选择任何项,无法删除", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
                    return;
                }

                dialogService.ShowDialog(
                    "AlertControlDialog",
                    new DialogParameters
                    {
                        { "AlertType", AlertType.Warning }, { "Title", "温馨提示" }, { "Message", "是否删除?删除后无法恢复" }
                    },
                    delegate(IDialogResult dialogResult)
                    {
                        if (dialogResult.Result == ButtonResult.Cancel)
                        {
                            return;
                        }

                        dataService.DeleteVelocity(_materialVelocity);
                        VelocityCollection = dataService.GetVelocityCollection(_materialName);
                    }
                );
                
                // var result = MessageBox.Show("是否删除?删除后无法恢复",
                //     "温馨提示", MessageBoxButton.OKCancel, MessageBoxImage.Question);
                // if (result == MessageBoxResult.Cancel)
                // {
                //     return;
                // }
                //
                // dataService.DeleteVelocity(_materialVelocity);
                // VelocityCollection = dataService.GetVelocityCollection(_materialName);
            });

            AddSpeedCommand = new DelegateCommand(delegate
            {
                dialogService.ShowDialog(
                    "AddSoundSpeedDialog",
                    new DialogParameters { { "MaterialName", _materialName } },
                    delegate { VelocityCollection = dataService.GetVelocityCollection(_materialName); }
                );
            });

            EditSpeedCommand = new DelegateCommand(delegate
            {
                if (_materialVelocity == null)
                {
                    dialogService.ShowDialog(
                        "AlertMessageDialog",
                        new DialogParameters
                        {
                            { "AlertType", AlertType.Error }, { "Title", "温馨提示" }, { "Message", "未选择任何项,无法编辑" }
                        },
                        delegate { }
                    );
                    // MessageBox.Show("未选择任何项,无法编辑", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
                    return;
                }

                dialogService.ShowDialog(
                    "EditSoundSpeedDialog",
                    new DialogParameters { { "MaterialVelocity", _materialVelocity } },
                    delegate { VelocityCollection = dataService.GetVelocityCollection(_materialName); }
                );
            });
        }

        public bool CanCloseDialog()
        {
            return true;
        }

        public void OnDialogClosed()
        {
        }

        public void OnDialogOpened(IDialogParameters parameters)
        {
            MaterialName = parameters.GetValue<string>("MaterialName");

            //查询数据库
            VelocityCollection = _dataService.GetVelocityCollection(_materialName);
        }
    }
}