How to get the current style

2 posts, 0 answers
  1. Chris
    Chris avatar
    11 posts
    Member since:
    Apr 2014

    Posted 30 Jun 2016 Link to this post

    I have a need to get the current style of a document. The GetCurrentSpanStyle method of RadRichTextBox is protected, so I cannot use this. The only way that I have been able to get the current style is to create a override of RichTextBoxCommandBase for this purpose.

    public class GetCurrentSpanStyleCommand : RichTextBoxCommandBase
        public GetCurrentSpanStyleCommand(RadRichTextBox editor) : base(editor)
        protected override void ExecuteOverride(object parameter)
        public Span ExecuteGetCurrentSpanStyle()
            return base.GetCurrentSpanStyle();

    Is there a better way to do this?

  2. Tanya
    Tanya avatar
    583 posts

    Posted 05 Jul 2016 Link to this post

    Hello Chris,

    The document could have many styles applied on its different document elements and I am not sure what exactly you need to access. I will list several of the options that might help you achieve the desired goal.

    - The default style of the document you could obtain through the Style property of RadDocument:
    StyleDefinition defaultDocumentStyle = this.radRichTextBox.Document.Style;

    - You could check the style of the Span in which the caret is currently positioned:
    StyleDefinition currentSpanStyle = this.radRichTextBox.Document.CaretPosition.GetCurrentSpanBox().AssociatedDocumentElement.Style;

    - All the styles that are used in the document could be accessed through the StyleRepository. The next snippet demonstrates how you could obtain the StyleDefinition for the Normal style:
    StyleDefinition normalStyle = this.radRichTextBox.Document.StyleRepository[RadDocumentDefaultStyles.NormalStyleName];

    More information on working with styles in RadRichTextBox you could find in our documentation.

    Hope this is helpful.

    Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
Back to Top