Example #1
0
        /// <summary>
        /// Creates a new dialog window that allows a user to edit a specific inventory item.
        /// </summary>
        /// <param name="item">The inventory item being edited.</param>
        public void ModifyItem(BaseInventoryModel item)
        {
            // Get the type of view model we are creating.
            Type viewModelType;

            // Get the type of model we need.
            if (item is MovieModel)
            {
                viewModelType = typeof(MovieModel);
            }
            else if (item is BookModel)
            {
                viewModelType = typeof(BookModel);
            }
            else if (item is VideoGameModel)
            {
                viewModelType = typeof(VideoGameModel);
            }
            else
            {
                throw new ArgumentOutOfRangeException(nameof(viewModelType), @"Unknown type for inventory model");
            }

            // Resolve (create) the view model.
            if (!(App.ServiceProvider.GetService(viewModelType) is BaseInventoryModel viewModel))
            {
                return;
            }

            // Copy the item's properties into the new model.
            viewModel.CopyFrom(item);

            // Create a new button to save changes to the item.
            UICommand saveChangesCommand = new UICommand()
            {
                Caption   = "Save",
                IsDefault = true,

                // Only allow the user to save their changes if there are no errors in the data entry.
                Command = new DelegateCommand(
                    () => { },
                    () => string.IsNullOrEmpty(viewModel.Error)
                    )
            };

            // Create a new button that cancels and closes the dialog window for modifying an item.
            UICommand cancelCommand = new UICommand()
            {
                Caption  = "Cancel",
                IsCancel = true,
            };

            // Display the dialog window.
            UICommand result = AddOrModifyItemDialogService?.ShowDialog(
                dialogCommands: new[] { saveChangesCommand, cancelCommand },
                title: "Modify an Item",
                viewModel: viewModel
                );

            // Check if the user executed the save command (clicked the save button).
            if (result != saveChangesCommand)
            {
                return;
            }

            // Copy the properties from the item we modified back into the original inventory item.
            item.CopyFrom(viewModel);
        }
Example #2
0
        // NOTE: The DevExpress POCO mechanism generates commands for all public methods without parameters or with a single parameter by convention.
        // Corresponding delegate command properties are created using the names of public methods with the suffix "Command".

        /// <summary>
        /// Creates a new dialog window that allows the user to add an item.
        /// </summary>
        /// <param name="itemType">The type of item is bound to the the tab the user is on (i.e adding from the movie tab creates a new movie).</param>
        public void AddItem(InventoryItemType itemType)
        {
            // Get the type of view model we are creating.
            Type viewModelType;

            switch (itemType)
            {
            case InventoryItemType.Book:
                viewModelType = typeof(BookModel);
                break;

            case InventoryItemType.Movie:
                viewModelType = typeof(MovieModel);
                break;

            case InventoryItemType.VideoGame:
                viewModelType = typeof(VideoGameModel);
                break;

            // Throw an exception if our item type does not match any of the models.
            default:
                throw new ArgumentOutOfRangeException();
            }

            // Resolve (create) the view model.
            if (!(App.ServiceProvider.GetService(viewModelType) is BaseInventoryModel viewModel))
            {
                return;
            }

            // Create a new button that adds the item.
            UICommand addItemCommand = new UICommand()
            {
                Caption   = "Add",
                IsDefault = true,

                // Only allow the user to add the item if there are no errors with the data entry.
                Command = new DelegateCommand(
                    () => { },
                    () => string.IsNullOrEmpty(viewModel.Error)
                    )
            };

            // Create a new button that cancels and closes the dialog window for adding an item.
            UICommand cancelCommand = new UICommand()
            {
                Caption  = "Cancel",
                IsCancel = true,
            };

            // Display the dialog window.
            UICommand result = AddOrModifyItemDialogService?.ShowDialog(
                dialogCommands: new[] { addItemCommand, cancelCommand },
                title: "Add a New Item",
                viewModel: viewModel
                );

            // Check if the user executed the add command (clicked the add button).
            if (result != addItemCommand)
            {
                return;
            }

            // Add the new model based on the type of item we are adding.
            switch (itemType)
            {
            case InventoryItemType.Book:
                BookModels.Add(viewModel as BookModel);
                break;

            case InventoryItemType.Movie:
                MovieModels.Add(viewModel as MovieModel);
                break;

            case InventoryItemType.VideoGame:
                VideoGameModels.Add(viewModel as VideoGameModel);
                break;

            // Throw an exception if our item type does not match any of the models.
            default:
                throw new ArgumentOutOfRangeException();
            }

            // Highlight the newly added item.
            FocusedItemModel = viewModel;
        }