/// <summary> /// This will be called in response to a user making a selection in the command bar/popup menu, which is were we want to update the model. We have provided /// some extensions that encapsulate this process which involves transactions. /// </summary> private static void ChangeTemperatureScale(ICommandParameter parameter, IEnumerable <IViewModel> selection, ICompositionHost host, DocumentEditSite site) { var fanSpeed = (FanSpeed)parameter.QueryService <ControlCommandParameter>().FirstOrDefault().Parameter; var models = selection.GetSelectedModels <FanModel>(); // useful extension to easily perform updates on models through transactions ITransactionManagerExtensions.TransactOnElements(models, "Change fan speed", model => model.FanSpeed = fanSpeed); }
/// <summary> /// Command handler which shows the data type of the selected terminal /// </summary> public static void OnShowTerminalType(ICommandParameter parameter, ICompositionHost host, DocumentEditSite site) { var viewModel = parameter.QueryService <NodeTerminalViewModel>().FirstOrDefault(); if (viewModel != null) { NIMessageBox.Show("The terminal type is: " + viewModel.DataType.ToString()); } }
private static void FanSpeedHighAttach(PlatformVisual visual, ICommandParameter parameter, IEnumerable <IViewModel> selection, ICompositionHost host, DocumentEditSite site) { if (!parameter.QueryService <ControlCommandParameter>().Any()) { parameter.AttachService(new ControlCommandParameter() { Parameter = FanSpeed.High }); } }
private static void FanSpeedLowAttach(PlatformVisual visual, ICommandParameter parameter, IEnumerable <IViewModel> selection, ICompositionHost host, DocumentEditSite site) { // This allows us to pass specific information to a common handler used by multiple commands. Otherwise, we would have redundant code for // three different commands if (!parameter.QueryService <ControlCommandParameter>().Any()) { parameter.AttachService(new ControlCommandParameter() { Parameter = FanSpeed.Low }); } }
private static void ChangeFanSpeedCombo(ICommandParameter parameter, IEnumerable <IViewModel> selection, ICompositionHost host, DocumentEditSite site) { var choiceParameter = parameter.QueryService <ChoiceCommandParameter>().FirstOrDefault(); if (choiceParameter != null) { var model = ((FanModel)((FanViewModel)selection.First()).Model); using (var transaction = model.TransactionManager.BeginTransaction("Change Spped", TransactionPurpose.User)) { model.FanSpeed = (FanSpeed)choiceParameter.Chosen; transaction.Commit(); } } }
/// <summary> /// This will be called when almost any change occurs in the editor (while the command bar with this command is active), and should be used to update /// the state of the command bar/popup menu editors themselves. For instance, in certain conditions you might want a particular editor disabled, or /// visually indicate that it is the option currently selected. For control commands, the two parameters we tend to be primarily interested in are /// the first two. The 'parameter' parameter is used to pass specific information to the handler to act on. The 'selection' parameter contains /// all of the models that are part of the current selection (assuming the necessary implementation exists to support multi-select for this model type, /// otherwise, this enumerable will contain one element when a single element is selected). Refrain from changing any model state here. /// </summary> private static bool CanChangeFanSpeed(ICommandParameter parameter, IEnumerable <IViewModel> selection, ICompositionHost host, DocumentEditSite site) { var controlParameter = parameter.QueryService <ControlCommandParameter>().FirstOrDefault(); if (controlParameter == null) { return(false); } // We can do this confidently based on our implementations of the AttachToVisualHandlers above var scale = (FanSpeed)controlParameter.Parameter; var selectedValue = selection.GetSelectedValue((FanModel model) => model.FanSpeed); // This is required to have the ribbon editor or popup menu visually indicate which option is current selected ((ICheckableCommandParameter)parameter).IsChecked = selectedValue == scale; /// If false the command associated with this callback would be disabled. For our commands, we never want a disabled option (assuming we got this far) return(true); }
private static bool CanChangeFanSpeedCombo(ICommandParameter parameter, IEnumerable <IViewModel> selection, ICompositionHost host, DocumentEditSite site) { var choiceParameter = parameter.QueryService <ChoiceCommandParameter>().FirstOrDefault(); if (choiceParameter != null) { if (choiceParameter.Choices == null || !choiceParameter.Choices.Any()) { var choices = new ChoiceCommandParameterChoice[] { new ChoiceCommandParameterChoice(FanSpeed.Low, PlatformImage.NullImage, "Low"), new ChoiceCommandParameterChoice(FanSpeed.Medium, PlatformImage.NullImage, "Medium"), new ChoiceCommandParameterChoice(FanSpeed.High, PlatformImage.NullImage, "High"), }; choiceParameter.Choices = choices; } choiceParameter.Chosen = ((FanModel)((FanViewModel)selection.First()).Model).FanSpeed; return(true); } return(false); }