在RichTextBox WPF中,设置内容的垂直对齐方式时出现问题。我尝试为文档设置“垂直内容对齐方式”和“ TextAlignment”,但仍然无法正常工作。垂直对齐方式仍然在顶部位置而不是在中心位置。
请找到代码示例作为设置对齐的参考,
FlowDocument mcFlowDoc = new FlowDocument();
// Create a paragraph with text
Paragraph para = new Paragraph();
para.Inlines.Add(new Run("Flow Document\n"));
para.Inlines.Add(new Bold(new Run("Content is not aligned\n.")));
para.Inlines.Add(new Run("Vertical content alignment does not work in RichTextBox\n"));
// Add the paragraph to blocks of paragraph
mcFlowDoc.Blocks.Add(para);
// Set contents
richtextbox.Document = mcFlowDoc;
//Set Alignment,
richtextbox.VerticalContentAlignment = VerticalAlignment.Center;
richtextbox.HorizontalContentAlignment = HorizontalAlignment.Center;
richtextboxt.Document.TextAlignment = TextAlignment.Center;
任何有兴趣的人,请问您对此有何建议?
答案 0 :(得分:0)
您可以尝试为FlowDocument而不是RichTextBox设置对齐方式。
看看下面的代码。
FlowDocument mcFlowDoc = new FlowDocument();
// Set alignment
mcFlowDoc.TextAlignment = TextAlignment.Center;
// Create a paragraph with text
Paragraph para = new Paragraph();
para.Inlines.Add(new Run("Flow Document\n"));
para.Inlines.Add(new Bold(new Run("Content is not aligned\n.")));
para.Inlines.Add(new Run("Vertical content alignment does not work in RichTextBox\n"));
// Add the paragraph to blocks of paragraph
mcFlowDoc.Blocks.Add(para);
// Set contents
richTextBox.Document = mcFlowDoc;
您还可以在Padding
控件上设置RichTextBox
属性,正如Kevin Pan提到的here