Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 670685
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T00:17:28+00:00 2026-05-14T00:17:28+00:00

I need to apply paragraph formatting to a selection in a rich text box.

  • 0

I need to apply paragraph formatting to a selection in a rich text box. My RTB will behave the same way as the rich text boxes on StackOverflow–the user can type text into the RTB, but they can also enter code blocks. The RTB will apply very simple formatting to the code block–it will change the font and apply a background color to the entire block, similar to what you see in the code block below.

Changing the font is pretty straightforward:

var textRange = new TextRange(rtb.Selection.Start, rtb.Selection.End);
textRange.ApplyPropertyValue(TextElement.FontFamilyProperty, "Consolas");
textRange.ApplyPropertyValue(TextElement.FontSizeProperty, 10D );

Now I need to apply some paragraph-level formatting. I need to set the paragraph margin to 0, so I don’t get a blank line between code lines, and I need to set the paragraph background color. Here’s my problem: I can’t figure out how to get the paragraph elements from the selection, so that I can apply formatting.

Any suggestions? An example of how to apply the Margin and Background properties would be incredibly helpful. Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T00:17:29+00:00Added an answer on May 14, 2026 at 12:17 am

    Oh, that was easy.. Came across the answer with a little more research:

    var textRange = new TextRange(TextBox.Selection.Start, TextBox.Selection.End);
    textRange.ApplyPropertyValue(TextElement.FontFamilyProperty, "Consolas");
    textRange.ApplyPropertyValue(TextElement.FontSizeProperty, 10D );
    textRange.ApplyPropertyValue(Paragraph.MarginProperty, new Thickness(0));
    textRange.ApplyPropertyValue(Paragraph.BackgroundProperty, "LightSteelBlue");
    

    The only limitation is that the highlighting still extends only as far as the text, rather than to the right side of the control. I’ll leave this question open for a couple of days; if someone can tell me how to extend the background to the right edge of the control, I’ll accept your answer to this question.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to apply some text formatting when displaying items in a WPF ListView.
I need to apply some xml templates to various streams of xml data (and
I need to apply a style in code ike this: TextBlock.Style = TryFindResource(MyStyle) as
My current program need to use programatically create a XPathExpression instance to apply to
Need a way to allow sorting except for last item with in a list.
I have a template: <xsl:template match=paragraph> ... </xsl:template> I call it: <xsl:apply-templates select=paragraph/> For
I created a macro that will apply a particular style to whatever is selected
I'm trying to apply the css on the a href, basically i need to
I've been running into the following case repeatedly lately, either I need to apply
I am using FieldSet element in my web application. Now I need to apply

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.