This is a migrated thread and some comments may be shown as answers.

Custom Command

8 Answers 327 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
Shaikh Ahmad
Top achievements
Rank 1
Shaikh Ahmad asked on 08 Jan 2011, 01:15 PM
How can i make a custom command in the RadRichTextBoxRibbonUI ?

8 Answers, 1 is accepted

Sort by
0
Accepted
Boby
Telerik team
answered on 11 Jan 2011, 10:39 AM
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>>
0
Shaikh Ahmad
Top achievements
Rank 1
answered on 15 Jan 2011, 04:55 AM
Can you show me how the XAML looks like ?
How do i bind command to XAML (not using MVVM just code behind) ?
0
Boby
Telerik team
answered on 18 Jan 2011, 10:08 AM
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>>
0
Shaikh Ahmad
Top achievements
Rank 1
answered on 25 Jan 2011, 08:20 AM
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?
0
Boby
Telerik team
answered on 25 Jan 2011, 06:01 PM
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>>
0
Shaikh Ahmad
Top achievements
Rank 1
answered on 26 Jan 2011, 12:30 AM
Good to hear that !
Thanks.
0
Anselmo
Top achievements
Rank 1
answered on 03 Sep 2012, 10:28 AM
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" />
0
Martin Ivanov
Telerik team
answered on 06 Sep 2012, 12:32 PM
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.

Tags
RichTextBox
Asked by
Shaikh Ahmad
Top achievements
Rank 1
Answers by
Boby
Telerik team
Shaikh Ahmad
Top achievements
Rank 1
Anselmo
Top achievements
Rank 1
Martin Ivanov
Telerik team
Share this question
or