/// <summary> /// Removes all the buttons from panel and dispose commands that support context. /// </summary> private void _ClearCommandButtons() { // Dispose commands that support context since they were instantiated by the class. foreach (Button button in ButtonsWrapPanel.Children) { Debug.Assert(button != null); // Obtain the command from the button. AppCommands.ICommand cmd = null; if (button is CommandButton) { cmd = ((CommandButton)button).ApplicationCommand; } else if (button is OptionsCommandButton) { cmd = ((OptionsCommandButton)button).ApplicationCommand; } else { Debug.Assert(false); // Not support type of button. } // If command supports context and can be disposed. if (cmd is AppCommands.ISupportContext && cmd is IDisposable) { // Dispose command. ((IDisposable)cmd).Dispose(); } } // Remove buttons from the panel. ButtonsWrapPanel.Children.Clear(); }
/// <summary> /// Creates command buttons. /// </summary> private void _CreateCommandButtons() { Debug.Assert(null != App.Current.CommandManager); _ClearCommandButtons(); // If command category name is empty - just clear command buttons. if (string.IsNullOrEmpty(CommandCategoryName)) { return; } ICollection <AppCommands.ICommand> commands = App.Current.CommandManager.GetCategoryCommands(CommandCategoryName); foreach (AppCommands.ICommand command in commands) { AppCommands.ICommand cmd = command; // If command supports context - special handling. if (command is AppCommands.ISupportContext) { // Instantiate new intance of such command to initialize it with the context. cmd = (AppCommands.ICommand)Activator.CreateInstance(command.GetType()); // Initialize command. cmd.Initialize(App.Current); // Set command context. ((AppCommands.ISupportContext)cmd).Context = CommandContext; } if (command is AppCommands.ISupportOptions) { OptionsCommandButton ocbtn = new OptionsCommandButton(); ocbtn.Content = command.Title; ocbtn.ApplicationCommand = cmd; ocbtn.Style = (Style)App.Current.FindResource("CommandOptionsButtonInGroupStyle"); ButtonsWrapPanel.Children.Add(ocbtn); } else { CommandButton cbtn = new CommandButton(); cbtn.Content = command.Title; cbtn.ApplicationCommand = cmd; cbtn.Style = (Style)App.Current.FindResource("CommandButtonInGroupStyle"); ButtonsWrapPanel.Children.Add(cbtn); } } }