Hello,
this post explains how to extend a xamRibbon ButtonTool. However, the ResolveToolControl method is called twice. Please see the example in the attachment. This is the reason of unexpected behavior of extended RadioButtonTool. Is there any way how to extend a RadioButtonTool to execute ICommands?
Thank you,
Alex
Please don't forget to implement Weak Event pattern to subscribe to the CanExecuteChanded event ;)
public class CustomRadioButtonTool : RadioButtonTool
{
public ICommand Command
get { return (ICommand)GetValue(CommandProperty); }
set { SetValue(CommandProperty, value); }
}
public static readonly DependencyProperty CommandProperty = DependencyProperty.Register("Command", typeof(ICommand), typeof(CustomRadioButtonTool), new PropertyMetadata(CommandChangedCallback));
private static void CommandChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs args)
var cmd = (CustomRadioButtonTool)d;
var command = args.NewValue as ICommand;
if (command != null)
command.CanExecuteChanged += cmd.OnCanExecuteChanged;
cmd.IsEnabled = command.CanExecute(cmd.CommandParameter);
if (args.OldValue != null)
((ICommand)args.OldValue).CanExecuteChanged -= cmd.OnCanExecuteChanged;
cmd.Command = command;
private void OnCanExecuteChanged(object sender, System.EventArgs e)
if (Command != null)
IsEnabled = Command.CanExecute(CommandParameter);
public object CommandParameter
get { return GetValue(CommandParameterProperty); }
set { SetValue(CommandParameterProperty, value); }
public static readonly DependencyProperty CommandParameterProperty = DependencyProperty.Register("CommandParameter", typeof(object), typeof(CustomRadioButtonTool), new PropertyMetadata(CommandParametersChangedCallback));
private static void CommandParametersChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
cmd.OnCanExecuteChanged(cmd, EventArgs.Empty);
protected override void OnToolClick()
base.OnToolClick();
Execute();
private void Execute()
if (Command != null && Command.CanExecute(CommandParameter))
Command.Execute(CommandParameter);
protected override RibbonToolBaseControl ResolveToolControl()
return new CustomRadioButtonToolControl(this);
Also it is necessary to create CustomRadioButtonToolControl because the setter of Tool property is internal.
public class CustomRadioButtonToolControl : RadioButtonToolControl
public CustomRadioButtonToolControl(CustomRadioButtonTool customRadioButtonTool)
Tool = customRadioButtonTool;
Thanks,
I have figured out what happened. ResolveToolControl method is called twice because there are two panels in a RibbonPageGroup - VerticalRibbonToolWrapPanel and HorizontalRibbonToolWrapPanel. It seems that RibbonToolBaseControl should be created for the each panel by design.