Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
290
xamRibbon ButtonTool: ResolveToolControl method is called twice
posted

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

TestButtonTool.zip
Parents
No Data
Reply
  • 290
    Verified Answer
    posted

     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)

            {

                var cmd = (CustomRadioButtonTool)d;

                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,

    Alex

Children