public NewFileDlg(NewFileDlgViewModel model)
 {
     InitializeComponent();
     Loaded += (sender, args) =>
     {
         var hwnd = new WindowInteropHelper(this).Handle;
         WindowHelper.SetWindowAsDialog(hwnd);
         DataContext = model;
     };
 }
        public SourcesManagerMainViewModel(IMaterialManager materialManager, IEventAggregator eventAggregator)
        {
            var config = new MapperConfiguration(cfg =>
            {
                cfg.CreateMap<Material, MaterialDto>()
                    .ForMember(dest => dest.Parent, opts =>
                        opts.MapFrom(s => s.Parent != null ? s.Parent.Id : (Guid?) null));
            });

            _map = config.CreateMapper();

            _materialManager = materialManager;
            _eventAggregator = eventAggregator;
            UpdateFromBase();

            _eventAggregator.GetEvent<DatabaseImportedEvent>().Subscribe(s => UpdateFromBase());

            ImportFileCommand = new DelegateCommand<MaterialDto>(item =>
            {
                if (item != null && !item.IsFolder)
                    return;

                var dlgModel = new AddFileDlgViewModel() {Material = new MaterialDto()
                {
                    IsFolder = false,
                    Parent = item != null ? item.Id : (Guid?) null
                }};

                var dlg = new AddFileDlg(dlgModel);
                var res = dlg.ShowDialog();
                if (res.HasValue && res.Value)
                {
                    var material = _materialManager.ImportNewFile(dlgModel.Material.Name,
                        dlgModel.Material.Author, dlgModel.Material.Source, dlgModel.Material.Parent, dlgModel.Path);
                    UpdateFromBase();
                    CurrentItem = Materials.FirstOrDefault(m => m.Id == material);
                }
            });

            NewFileCommand = new DelegateCommand<MaterialDto>(item =>
            {
                if (item != null && !item.IsFolder)
                    return;

                var dlgModel = new NewFileDlgViewModel()
                {
                    Material = new MaterialDto()
                    {
                        IsFolder = false,
                        Parent = item != null ? item.Id : (Guid?)null
                    }
                };

                var dlg = new NewFileDlg(dlgModel);
                var res = dlg.ShowDialog();
                if (res.HasValue && res.Value)
                {
                    var material = _materialManager.CreateNewFile(dlgModel.Material.Name,
                        dlgModel.Material.Author, dlgModel.Material.Source, dlgModel.Material.Parent);
                    UpdateFromBase();
                    CurrentItem = Materials.FirstOrDefault(m => m.Id == material);
                }
            });

            NewFolderCommand = new DelegateCommand<MaterialDto>(o =>
            {
                var added = _materialManager.AddFolder(o == null ? null : (Guid?)o.Id, "New folder");
                UpdateFromBase();
                CurrentItem = Materials.FirstOrDefault(m => m.Id == added);
                OnRenameFolder();
            });

            RenameCommand = new DelegateCommand(OnRenameFolder);

            OpenMaterialCommand = new DelegateCommand<MaterialDto>(item =>
            {
                if (item != null && !item.IsFolder)
                    Interfaces.CompositeCommands.OpenSourceCommand.Execute(item.Id);
            });

            DeleteCommand = new DelegateCommand<MaterialDto>(item =>
            {
                var btnMessageBox = MessageBoxButton.YesNoCancel;
                var icnMessageBox = MessageBoxImage.Warning;

                MessageBoxResult rsltMessageBox =
                    MessageBox.Show(String.Format("Вы действительно хотите удалить {0}", item.Name), "Удалить материал",
                        btnMessageBox, icnMessageBox);

                if (rsltMessageBox == MessageBoxResult.Yes)
                {
                    _materialManager.DeleteMaterial(item.Id);
                    UpdateFromBase();
                }
            });
        }