How exactly do you change the Font in a RichTextBox?
Looking around gives me old answers that doesn't seem to work any more. I thought it would be as simple as doing richtextbox1.Font = Font.Bold;
or something similar. Turns out it's not, so I looked around. Apparently you have to change the FontStyle
which is a readonly
(??) property, but you have to do it making a new FontStyle
Object.
But even then that doesn't work o.o
How do you do this? EDIT:
Doesn't seem to work :\
rssTextBox.Document.Blocks.Clear();
rssTextBox.FontWeight = FontWeights.Bold;
rssTextBox.AppendText("Title: ");
rssTextBox.FontWeight = FontWeights.Normal;
rssTextBox.AppendText(rs.Title + "\n");
rssTextBox.FontWeight = FontWeights.Bold;
rssTextBox.AppendText("Publication Date: ");
rssTextBox.FontWeight = FontWeights.Normal;
rssTextBox.AppendText(rs.PublicationDate + "\n");
rssTextBox.FontWeight = FontWeights.Bold;
rssTextBox.AppendText("Description: ");
rssTextBox.FontWeight = FontWeights.Normal;
rssTextBox.AppendText(rs.Description + "\n\n");
Bold
is a FontWeight
. You can apply it directly.
As MSDN Doc states "Gets or sets the weight or thickness of the specified font."
You can either set it in xaml
<RichTextBox FontWeight="Bold" x:Name="richText" />
or in codebehind:
richText.FontWeight = FontWeights.Bold;
If your trying to switch FontFamily
that would be like:
richText.FontFamily = new FontFamily("Arial");
or FontStyle
:
richText.FontStyle = FontStyles.Italic;
Update: (for updating RichTextBox
inline)
This is just a quick mock-up. Using this as an example. Please structure it for your requirements.
richText.Document.Blocks.Clear();
Paragraph textParagraph = new Paragraph();
AddInLineBoldText("Title: ", ref textParagraph);
AddNormalTextWithBreak(rs.Title, ref textParagraph);
AddInLineBoldText("Publication Date: ", ref textParagraph);
AddNormalTextWithBreak(rs.PublicationDate, ref textParagraph);
AddInLineBoldText("Description: ", ref textParagraph);
AddNormalTextWithBreak(rs.Description, ref textParagraph);
AddNormalTextWithBreak("", ref textParagraph);
richText.Document.Blocks.Add(textParagraph);
private static void AddInLineBoldText(string text, ref Paragraph paragraph) {
Bold myBold = new Bold();
myBold.Inlines.Add(text);
paragraph.Inlines.Add(myBold);
}
private static void AddNormalTextWithBreak(string text, ref Paragraph paragraph) {
Run myRun = new Run {Text = text + Environment.NewLine};
paragraph.Inlines.Add(myRun);
}
来源:https://stackoverflow.com/questions/16125870/decide-fontstyle-bold-italic-underlined-for-richtextbox