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);
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.
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.
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; }
}
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.
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);
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.
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.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
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.
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.
<telerik:RadRichTextBox DocumentInheritsDefaultStyleSettings="True" Foreground="black" FontFamily="{Binding DefaultFontType}" FontSize="{Binding DefaultFontSize}" > </telerik:RadRichTextBox>
This is default fontfamily and size.
This is default spacing:
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;
Code below:
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.
Once we have added the provider, it ignores the style. So I am wondering if I need to target a different style.
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.
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
Hi Patrick,
I am glad that all issues are now resolved.
Do not hesitate to contact us if you have other questions.