Binding to RadRichTextBox FontFamily and FontSize.

0 Answers 66 Views
RichTextBox
Patrick
Top achievements
Rank 1
Iron
Iron
Iron
Patrick asked on 10 May 2024, 02:12 PM
How can I bind a property to RadRichTextBox FontSize and FontFamily? I get errors saying the property types don't match but they do. FontSize property is double and FontFamily property type is FontFamily. Any ideas?
Dimitar
Telerik team
commented on 13 May 2024, 07:16 AM

Hi Patrick, 

I am not sure that binding in this case will have the desired effect. In this case, setting this will only alter the default values. I would recommend using the RadDocumentEditor methods to change the size and the font family. 

var rtb = new RadRichTextBox();
rtb.ActiveDocumentEditor.ChangeFontFamily(new FontFamily("Arial"));
rtb.ChangeFontSize(20);

Patrick
Top achievements
Rank 1
Iron
Iron
Iron
commented on 13 May 2024, 04:29 PM

I need to change the default values. Some of the RTB's will be different fontsizes and font families. Generated multiple times using the same User control. 

However, the code you posted is great because that helps me solve another issue that I will need to deal with in the future when user copy and pastes text that is not the proper font size or font family.
Dimitar
Telerik team
commented on 14 May 2024, 07:57 AM

Hi Patrick, 

These properties are inherited from the Control class and you should be able to bind them. Please make sure that the DocumentInheritsDefaultStyleSettings property is set to true as well. 

I hope this helps. 
Patrick
Top achievements
Rank 1
Iron
Iron
Iron
commented on 14 May 2024, 07:53 PM

DocumentInheritsDefaultStyleSettings is set to true. The same UserControl may have different default FontSize and FontFamily.

When I set up the binding to the property I get a message below:

System.Windows.Data Error: 5 : Value produced by BindingExpression is not valid for target property. null BindingExpression:Path=MyFontSize; DataItem='MyModel' (HashCode=19250065); target element is 'RadRichTextBox' (Name='myRadRichTextBox'); target property is 'FontSize' (type 'Double')

My model property is a double. This is an error I've seen many times in other places when my binding properties don't match. What am I missing here? The FontFamily is the same error. 

System.Windows.Media.FontFamily - this is the property type for the FontFamily

double - this is the property type for the FontSize

Dimitar
Telerik team
commented on 15 May 2024, 11:11 AM

Hi Patrick, 

Can you attach your project (or a sample project)  so I can examine it and see if I can pinpoint why this is not working?

Thank you in advance for your patience and cooperation. 

Patrick
Top achievements
Rank 1
Iron
Iron
Iron
commented on 26 Jul 2024, 07:05 PM

Hey Dimitar, I cannot attach a sample project. I am not allowed to share any specific code.

I can explain. I am dynamically generating many rad rich textboxes from a model. I need some of the radrich text boxes to have different default fontsize and fontfamily. My RichTextBox is in its own user control.

I have a model for each box to set options, ID, etc. A sample model is below.

 


public class TestControl
{
   
    public string Name { get; set; }
    public string DefaultFontType { get; set; }
    public double? DefaultFontSize { get; set; }
}

<telerik:RadRichTextBox  DocumentInheritsDefaultStyleSettings="True" Foreground="black" FontFamily="{Binding DefaultFontType}" FontSize="{Binding DefaultFontSize}" >
</telerik:RadRichTextBox>


Patrick
Top achievements
Rank 1
Iron
Iron
Iron
commented on 26 Jul 2024, 07:21 PM

I also need all the line spacing for all of these RadRichTextBox to be set to single space, 0 point before and 0 point after.


This is default fontfamily and size.

This is default spacing:
Dimitar
Telerik team
commented on 29 Jul 2024, 05:30 AM

Hi Patrick, 

It would be best if you change the default spacing values in the default style: 

var normalStyle = this.radRichTextBox.Document.StyleRepository[RadDocumentDefaultStyles.NormalStyleName];  
normalStyle.ParagraphProperties.SpacingAfter = 0;
normalStyle.ParagraphProperties.SpacingBefore = 0;
normalStyle.ParagraphProperties.LineSpacingType = LineSpacingType.Exact;
normalStyle.ParagraphProperties.LineSpacing = 0;

Patrick
Top achievements
Rank 1
Iron
Iron
Iron
commented on 29 Jul 2024, 03:09 PM

I've already got this set in my code behind. This has no impact on the line spacing.

Code below:


public MyRichTextBox()
{
    InitializeComponent();

    StyleDefinition normalStyle = this.radRichTextBox.Document.StyleRepository[RadDocumentDefaultStyles.NormalStyleName];
    normalStyle.ParagraphProperties.AutomaticSpacingAfter = false;
    normalStyle.ParagraphProperties.SpacingAfter = 0;
    normalStyle.ParagraphProperties.SpacingBefore = 0;

    this.radRichTextBox.ActiveDocumentEditor.ChangeParagraphSpacingAfter(0);
    this.radRichTextBox.ActiveDocumentEditor.ChangeParagraphSpacingBefore(0);
    this.radRichTextBox.ActiveDocumentEditor.ChangeParagraphLineSpacing(0, LineSpacingType.Exact);

}

Dimitar
Telerik team
commented on 30 Jul 2024, 09:02 AM

Hi Patrick, 

I cannot say why this is not working without looking at the document. I would recommend attaching your document in a new ticket which is a private thread. This will alow us to properly investigate the case. 

Thank you for your understanding.

Patrick
Top achievements
Rank 1
Iron
Iron
Iron
commented on 30 Jul 2024, 06:14 PM

Hey Dimitar, I have found a clue. My richTextBox uses a XAMLDataProvider. The code above that you provided works great without the provider.

Once we have added the provider, it ignores the style. So I am wondering if I need to target a different style.
Dimitar
Telerik team
commented on 31 Jul 2024, 09:16 AM

Hi Patrick, 

Without the document, I cannot say why this is not working. Would it be possible to share a sample document?

I am looking forward to your reply.

Patrick
Top achievements
Rank 1
Iron
Iron
Iron
commented on 31 Jul 2024, 06:39 PM | edited

Hey Dimitar, I am sorry. Attaching a solution and/or document is just not a possibility for me.

But I have some good news and bad news.

Good News:

I am successfully binding to FontSize and FontFamily.

I am setting line spacing on the XAMLDataProvider Loaded event which seems to be working perfectly.

Bad News with good outcome:

I am setting default font for some items to 12 and some to 10.

When I open font in application to check, it tells me that the font size is either 9 or smaller.

Looks like the issue is that the fontSize on a RadRichTextBox is not the same as an actual document which caused me some grief and time to figure out.

So in my converter, I am taking my font size and return converting it. So now it is working properly. Very odd but all issues have been worked out.

double RealFontSize = Unit.PointToDip(myFontSize)
return RealFontSize 
Dimitar
Telerik team
commented on 01 Aug 2024, 08:42 AM

Hi Patrick, 

I am glad that all issues are now resolved.

Do not hesitate to contact us if you have other questions.

No answers yet. Maybe you can help?

Tags
RichTextBox
Asked by
Patrick
Top achievements
Rank 1
Iron
Iron
Iron
Share this question
or