In my simple Silverlight class assignment...
the last method :

FontFamily verdanaFnt= new FontFamily("Verdana");

myRichTextBox.Selection.ApplyPropertyValue (Run.FontFamilyProperty,verdanaFnt)

The problem is that it changes the FontFamily of the whole RTB including the non-selected parts.

It behaves kinda like:

myRichTextBox.FontFamily=verdanaFnt;

What i am I doing wrong?

Dani AI

Generated

The symptom (changing the whole box instead of just the highlighted text) usually comes from using the wrong dependency property and/or applying formatting when the selection is empty. used a Run-based property; replace that with the text-element property that the RichTextBox selection expects. Also note that 's SelectionFont suggestion is for WinForms RichTextBox (System.Windows.Forms) and does not apply to Silverlight/WPF RichTextBox.

Example (Silverlight/WPF):

using System.Windows.Documents;

if (!myRichTextBox.Selection.IsEmpty)
{
    myRichTextBox.Selection.ApplyPropertyValue(TextElement.FontFamilyProperty, new FontFamily("Verdana"));
}

Troubleshooting notes:

  • If Selection.IsEmpty is true, ApplyPropertyValue sets the typing/default formatting at the caret; that can appear to change large portions of text if most text inherits default formatting. Always check IsEmpty before applying if the intent is to affect only an explicit selection.
  • If selection spans multiple Paragraphs/Runs with mixed formatting, ApplyPropertyValue normalizes the selection. Inspect the document (myRichTextBox.Document / its Paragraphs and Runs) to see how formatting is applied and, if needed, split or wrap the selection into Runs and set the Run's FontFamily.
  • Remember namespaces: TextElement lives under System.Windows.Documents. The Silverlight/WPF model is different from WinForms; use ApplyPropertyValue with TextElement.* properties for inline formatting.

This addresses the code already shown in the thread and clarifies why the WinForms approach offered by won't behave the same in Silverlight.

Recommended Answers

All 2 Replies

myRichTextBox.SelectionFont = new Font( currentFont.FontFamily, currentFont.Size, newFontStyle);

try using this instead of line# 3

you can also check

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.