[英]Setting WPF text to TextBlock
I know that TextBlock
can present a FlowDocument
, for example: 我知道
TextBlock
可以呈现FlowDocument
,例如:
<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
. 我想知道如何将存储在变量中的
FlowDocument
设置为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. 但是,上面代码的结果是XAML文本显示为未解析。
EDIT: I guess my question was not clear enough. 编辑:我想我的问题不够明确。 What I'm really trying to achive is:
我真正想要达到的目的是:
FlowDocument
from the RichTextBox , and serializes it to the disk. FlowDocument
,并将其序列化到磁盘。 FlowDocument
is deserialized from the disk to the variable text . FlowDocument
从磁盘反序列化为变量文本 。 TextBlock
. TextBlock
显示用户文本。 Therefore, as far as I understand, creating a new Run object and setting the parameters manually will not solve my problem. 因此,据我所知,创建一个新的Run对象并手动设置参数将无法解决我的问题。
The problem is that serializing RichTextBox creates Section object, which I cannot add to TextBlock.Inlines . 问题是序列化RichTextBox会创建Section对象,我无法将其添加到TextBlock.Inlines 。 Therefore, it is not possible to set the deserialized object to TextProperty of TextBlock .
因此,无法将反序列化的对象设置为TextBlock的 TextProperty 。
create and add the object as below: 创建并添加对象如下:
Run run = new Run("Courier New 24");
run.Foreground = new SolidColorBrush(Colors.Maroon);
run.FontFamily = new FontFamily("Courier New");
run.FontSize = 24;
txtFont.Inlines.Add(run);
I know that TextBlock can present FlowDocument
我知道TextBlock可以呈现FlowDocument
What makes you think that ? 什么让你有那个想法 ? I don't think it's true... The content of a
TextBlock
is the Inlines
property, which is an InlineCollection
. 我不认为这是真的......
TextBlock
的内容是Inlines
属性,它是一个InlineCollection
。 So it can only contain Inline
s... But in a FlowDocument
, the content is the Blocks
property, which contains instances of Block
. 所以它只能包含
Inline
s ...但是在FlowDocument
,内容是Blocks
属性,它包含Block
实例。 And a Block
is not an Inline
并且
Block
不是Inline
If your FlowDocument has been deserialized , it means that you have an object of type FlowDocument
, right? 如果您的FlowDocument已反序列化 ,则意味着您有一个
FlowDocument
类型的对象,对吧? Try setting the Text property of your TextBlock to this value. 尝试将TextBlock的Text属性设置为此值。 Of course, you cannot do this with
txtFont.Text = ...
, since this only works for strings. 当然,你不能用
txtFont.Text = ...
来做到这一点,因为这只适用于字符串。 For other types of objects, you need to set the DependencyProperty directly: 对于其他类型的对象,您需要直接设置DependencyProperty:
txtFont.SetValue(TextBlock.TextProperty, myFlowDocument)
Here is how we are setting the look of a textblock by assigning a style on-the-fly. 以下是我们如何通过动态分配样式来设置文本块的外观。
// Set Weight (Property setting is a string like "Bold")
FontWeight thisWeight = (FontWeight)new FontWeightConverter().ConvertFromString(Properties.Settings.Default.DealerMessageFontWeightValue);
// Set Color (Property setting is a string like "Red" or "Black")
SolidColorBrush thisColor = (SolidColorBrush)new BrushConverter().ConvertFromString(Properties.Settings.Default.DealerMessageFontColorValue);
// Set the style for the dealer message
// Font Family Property setting is a string like "Arial"
// Font Size Property setting is an int like 12, a double would also work
Style newStyle = new Style
{
TargetType = typeof(TextBlock),
Setters = {
new Setter
{
Property = Control.FontFamilyProperty,
Value = new FontFamily(Properties.Settings.Default.DealerMessageFontValue)
},
new Setter
{
Property = Control.FontSizeProperty,
Value = Properties.Settings.Default.DealerMessageFontSizeValue
},
new Setter
{
Property = Control.FontWeightProperty,
Value = thisWeight
},
new Setter
{
Property = Control.ForegroundProperty,
Value = thisColor
}
}
};
textBlock_DealerMessage.Style = newStyle;
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. 您可以直接删除样式部分和设置属性,但我们喜欢将样式捆绑在一起以帮助我们整理项目中的外观。
textBlock_DealerMessage.FontWeight = thisWeight;
HTH. HTH。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.