将 WPF 文本设置为 TextBlock
我知道 TextBlock
可以呈现一个 FlowDocument
,例如:
<TextBlock Name="txtFont">
<Run Foreground="Maroon" FontFamily="Courier New" FontSize="24">Courier New 24</Run>
</TextBlock>
我想知道如何将存储在变量中的 FlowDocument
设置为一个TextBlock
。 我正在寻找类似的内容:
string text = "<Run Foreground="Maroon" FontFamily="Courier New" FontSize="24">Courier New 24</Run>"
txtFont.Text = text;
但是,上面代码的结果是 XAML 文本未解析。
编辑:我想我的问题不够清楚。我真正想要实现的是:
- 用户将一些文本输入到 RichTextBox 中。
- 应用程序将 RichTextBox 中的用户输入保存为
FlowDocument
,并将其序列化到磁盘。 FlowDocument
从磁盘反序列化为变量text。- 现在,我希望能够在
TextBlock
中呈现用户文本。
因此,据我了解,创建一个新的 Run 对象并手动设置参数并不能解决我的问题。
问题是序列化 RichTextBox 创建 Section 对象,我无法将其添加到 TextBlock.Inlines 中。因此,无法将反序列化对象设置为 TextBlock 的 TextProperty。
I know that TextBlock
can present a FlowDocument
, for example:
<TextBlock Name="txtFont">
<Run Foreground="Maroon" FontFamily="Courier New" FontSize="24">Courier New 24</Run>
</TextBlock>
I would like to know how to set a FlowDocument
that is stored in a variable to a TextBlock
.
I am looking for something like:
string text = "<Run Foreground="Maroon" FontFamily="Courier New" FontSize="24">Courier New 24</Run>"
txtFont.Text = text;
However, The result of the code above is that the XAML text is presented unparsed.
EDIT: I guess my question was not clear enough. What I'm really trying to achive is:
- The user input some text into a RichTextBox.
- The application saves the user input as
FlowDocument
from the RichTextBox, and serializes it to the disk. - The
FlowDocument
is deserialized from the disk to the variable text. - Now, I would like to be able to present the user text in a
TextBlock
.
Therefore, as far as I understand, creating a new Run object and setting the parameters manually will not solve my problem.
The problem is that serializing RichTextBox creates Section object, which I cannot add to TextBlock.Inlines. Therefore, it is not possible to set the deserialized object to TextProperty of TextBlock.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
创建并添加对象如下:
create and add the object as below:
您为什么这么认为?我不认为这是真的...
TextBlock
的内容是Inlines
属性,它是一个InlineCollection
。所以它只能包含Inline
...但是在FlowDocument
中,内容是Blocks
属性,其中包含的实例阻止。并且
Block
不是Inline
What makes you think that ? I don't think it's true... The content of a
TextBlock
is theInlines
property, which is anInlineCollection
. So it can only containInline
s... But in aFlowDocument
, the content is theBlocks
property, which contains instances ofBlock
. And aBlock
is not anInline
如果您的FlowDocument已经反序列化,则意味着您有一个
FlowDocument
类型的对象,对吗?尝试将 TextBlock 的 Text 属性设置为此值。当然,您不能使用 txtFont.Text = ... 来执行此操作,因为这只适用于字符串。对于其他类型的对象,需要直接设置DependencyProperty:If your FlowDocument has been deserialized, it means that you have an object of type
FlowDocument
, right? Try setting the Text property of your TextBlock to this value. Of course, you cannot do this withtxtFont.Text = ...
, since this only works for strings. For other types of objects, you need to set the DependencyProperty directly:以下是我们如何通过动态分配样式来设置文本块的外观。
您可以消除样式部分并直接设置属性,但我们喜欢将东西捆绑在样式中,以帮助我们组织整个项目的外观。
HTH。
Here is how we are setting the look of a textblock by assigning a style on-the-fly.
You can eliminate the style section and set properties directly, but we like keeping things bundled in the style to help us organize the look throughout the project.
HTH.