决定fontstyle的(粗体,斜体,下划线)为RichTextBox的 [英] Decide FontStyle (Bold, Italic, Underlined) for RichTextBox

查看:757
本文介绍了决定fontstyle的(粗体,斜体,下划线)为RichTextBox的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何你到底改变字体在一个RichTextBox?



放眼望去给了我老的答案似乎没有工作了。我认为这将是因为这样做 richtextbox1.Font = Font.Bold一样简单; 或类似的东西。原来它不是,所以我环顾四周。显然,你必须修改 fontstyle的这是一个只读(?)的财产,但你必须这样做使一个新的 fontstyle的对象。



但即便如此,这并不正常工作OO



你怎么做到这一点?
编辑:



似乎不工作:\

  rssTextBox.Document.Blocks.Clear(); 
rssTextBox.FontWeight = FontWeights.Bold;
rssTextBox.AppendText(标题:);
rssTextBox.FontWeight = FontWeights.Normal;
rssTextBox.AppendText(rs.Title +\\\
);
rssTextBox.FontWeight = FontWeights.Bold;
rssTextBox.AppendText(出版日期:);
rssTextBox.FontWeight = FontWeights.Normal;
rssTextBox.AppendText(rs.PublicationDate +\\\
);
rssTextBox.FontWeight = FontWeights.Bold;
rssTextBox.AppendText(描述:);
rssTextBox.FontWeight = FontWeights.Normal;
rssTextBox.AppendText(rs.Description +\\\
\\\
);


解决方案

粗体粗细。您可以直接使用它。



作为的 MSDN文档国家获取或设置的权重,或指定字体的厚度。



您既可以其设置在XAML

 < RichTextBox的粗细=大胆X:NAME =富文本/> 

或代码隐藏:

  richText.FontWeight = FontWeights.Bold; 

如果您尝试切换的FontFamily 那会如:

  richText.FontFamily =新的FontFamily(宋体); 

fontstyle的

  richText.FontStyle = FontStyles.Italic; 



更新:(更新的RichTextBox 在线)



这仅仅是一个快速实体模型。使用这个作为一个例子。请构建它为您的要求。



  richText.Document.Blocks.Clear(); 
段textParagraph =新的第();
AddInLineBoldText(标题:裁判textParagraph);
AddNormalTextWithBreak(rs.Title,楼盘textParagraph);
AddInLineBoldText(出版日期:裁判textParagraph);
AddNormalTextWithBreak(rs.PublicationDate,楼盘textParagraph);
AddInLineBoldText(描述:裁判textParagraph);
AddNormalTextWithBreak(rs.Description,楼盘textParagraph);
AddNormalTextWithBreak(,参考textParagraph);
richText.Document.Blocks.Add(textParagraph);

私有静态无效AddInLineBoldText(字符串文本,文献逐段){
黑体myBold =新款Bold();
myBold.Inlines.Add(文本);
paragraph.Inlines.Add(myBold);
}

私有静态无效AddNormalTextWithBreak(字符串文本,文献逐段){
运行myRun =新的运行{文字=文字+ Environment.NewLine};
paragraph.Inlines.Add(myRun);
}


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);
}

这篇关于决定fontstyle的(粗体,斜体,下划线)为RichTextBox的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆