Custom Command

9 posts, 1 answers
  1. Shaikh Ahmad
    Shaikh Ahmad avatar
    20 posts
    Member since:
    Oct 2012

    Posted 08 Jan 2011 Link to this post

    How can i make a custom command in the RadRichTextBoxRibbonUI ?
  2. Answer
    Boby
    Admin
    Boby avatar
    763 posts

    Posted 11 Jan 2011 Link to this post

    Hello Shaikh Ahmad,
    RadRichTextBoxRibbonUI is indeed similar to RadRibbonBar which, when added to the VS Designer surface, is filled with predefined set of controls - RadButtons, RadToggleButtons, RadComboBoxes etc. As these controls have Command property of type ICommand, you can just create your command by implementing System.Windows.Input.ICommand and set it to this property.
    You can also take advantage of our base class - RichTextBoxCommandBase. With it for example you can have your command automatically disabled when its associated RadRichTextBox is read-only (its IsReadOnly property is true). (This base class also encapsulates behavior for updating the state of the control that uses the command - you can read more about it and RichTextCommand attached property in this blog post.)
    There are some methods and properties you can override here:
    • CanExecuteOverride - check if the command is enabled here - for example, DeleteTableRow command checks if the current caret position is inside table.
    • ExecuteOverride - put your execution logic here.
    • CanExecuteInReadOnlyMode - determines if command should be enabled when its associated RadRichTextBox is in read-only mode. Default implementation return false.
    • UpdateSpanStyle - this method is called when the current style under caret/selection changes (for example when caret is moving from bold to not bold text). We use this method in the special case that we need to raise events for updating the state of the control that is bound to the command.
    Considering your other question in the forum, here is a sample code for command that inserts table and sets font family and size:
    public class InsertLargeFontTableCommand : InsertTableCommand
    {
        public InsertLargeFontTableCommand(RadRichTextBox radRichTextBox)
            : base(radRichTextBox)
        {
        }
     
        protected override void ExecuteOverride(object parameter)
        {
            this.AssociatedRichTextBox.InsertTable(2, 5);
            this.AssociatedRichTextBox.ChangeFontSize(40);
            this.AssociatedRichTextBox.ChangeFontFamily(new FontFamily("Courier New"));
        }
    }

    Don't hesitate to contact us if you need further assistance.

    Best wishes,
    Boby
    the Telerik team
    Let us know about your Windows Phone 7 application built with RadControls and we will help you promote it. Learn more>>
  3. Shaikh Ahmad
    Shaikh Ahmad avatar
    20 posts
    Member since:
    Oct 2012

    Posted 14 Jan 2011 Link to this post

    Can you show me how the XAML looks like ?
    How do i bind command to XAML (not using MVVM just code behind) ?
  4. Boby
    Admin
    Boby avatar
    763 posts

    Posted 18 Jan 2011 Link to this post

    Hi Shaikh Ahmad,
    The code for binding controls to commands depends from  the type of the controls, but assuming the controls is RadButton, and using the sample command from my previous post, you can use the Command property:
    this.buttonInsertLargeFontTable.Command = new InsertLargeFontTableCommand(this.radRichTextBox);
    Get back here if this is not the case or you need further assistance.

    Kind regards,
    Boby
    the Telerik team
    Let us know about your Windows Phone 7 application built with RadControls and we will help you promote it. Learn more>>
  5. Shaikh Ahmad
    Shaikh Ahmad avatar
    20 posts
    Member since:
    Oct 2012

    Posted 25 Jan 2011 Link to this post

    Since i'm currently using code behind for development, I've decided to create a button and manipulate the behavior directly. Is there and problem with my approach?
  6. Boby
    Admin
    Boby avatar
    763 posts

    Posted 25 Jan 2011 Link to this post

    Hello Shaikh Ahmad,

    There is no problem, it is your choice - actually the bound command is executed when the button is clicked, so you can just subscribe to the Click event and implement your logic in the event handler.

    All the best,
    Boby
    the Telerik team
    Let us know about your Windows Phone 7 application built with RadControls and we will help you promote it. Learn more>>
  7. Shaikh Ahmad
    Shaikh Ahmad avatar
    20 posts
    Member since:
    Oct 2012

    Posted 25 Jan 2011 Link to this post

    Good to hear that !
    Thanks.
  8. Anselmo
    Anselmo  avatar
    5 posts
    Member since:
    Aug 2012

    Posted 03 Sep 2012 Link to this post

    Hi Boby,

    We are instructed to implement the code in the view model, so how do you set the command in the XAML.

    Ive already created a class for a custom PasteCommand and Ive created a ViewModelLocator as a StaticResource in the App.xml to be used in the Xaml markup as shown below:

    When I click Copy and then Paste nothing happens on the Editor. Im thinking that the parameter for the PasteCommand is not being passed on the telerik:RadRibbonButton. So how I can do this?
     



    public class DocumentPasteCommand : PasteCommand
    {
        public DocumentPasteCommand(RadRichTextBox radRichTextBox)
            : base(radRichTextBox)
        {
        }
     
        protected override void ExecuteOverride(object parameter)
        {
            this.AssociatedRichTextBox.InsertTable(2, 5);
            this.AssociatedRichTextBox.ChangeFontSize(40);
            this.AssociatedRichTextBox.ChangeFontFamily(new FontFamily("Courier New"));
        }
    }

    public class ViewModelLocator
       {
           /// <summary>
           /// Initializes a new instance of the ViewModelLocator class.
           /// </summary>
           public ViewModelLocator()
           {
               ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);
     
               
     
               SimpleIoc.Default.Register<DocumentSectionManager>();
               SimpleIoc.Default.Register<DocumentPasteCommand>();
     
           }
     
     
           public DocumentSectionManager DocMaker
           {
               get
               {
                   return ServiceLocator.Current.GetInstance<DocumentSectionManager>();
               }
           }
     
           public DocumentPasteCommand cmdPaste
           {
               get
               {
                   return ServiceLocator.Current.GetInstance<DocumentPasteCommand>();
               }
           }
     
           public static void Cleanup()
           {
               // TODO Clear the ViewModels
           }
       }

    <telerik:RadRibbonButton CollapseToSmall="WhenGroupIsMedium" Command="{Binding Source={StaticResource Locator}, Path=cmdPaste}" telerik:ScreenTip.Description="Paste the contents of the Clipboard." telerik:ScreenTip.Title="Paste" Size="Medium" SmallImage="/Telerik.Windows.Controls.RichTextBoxUI;component/Images/MSOffice/16/paste.png" Text="Paste" />
  9. Martin Ivanov
    Admin
    Martin Ivanov avatar
    104 posts

    Posted 06 Sep 2012 Link to this post

    Hi Anselmo,
    We are not sure we understand your scenario; could you specify what are you trying to achieve? It is normal that the command does not receive a parameter since you haven't set the CommandParameter property on your RadRibbonButton in XAML.
    On the other side, the original paste command works with the clipboard and don't need a parameter. Maybe the problem is with the binding, so you could check your ServiceLocator class and the static resource.

    All the best,
    Martin
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

Back to Top