我的WPF窗口中有一个TextBlock.
<TextBlock>
Some <Bold>formatted</Bold> text.
</TextBlock>
Run Code Online (Sandbox Code Playgroud)
渲染时它看起来像这样,
一些格式化文本.
我的问题是,我可以将此内联"内容"绑定到我的应用程序中的资源吗?
我得到了:
制作应用程序资源字符串,
myText="Some <Bold>formatted</Bold> text."
Run Code Online (Sandbox Code Playgroud)
和以下xaml(为简洁起见,省略了一些代码)
<Window xmlns:props="clr-namespace:MyApp.Properties">
<Window.Resources>
<props:Resources x:Key="Resources"/>
</Window.Resources>
<TextBlock x:Name="Try1"
Text="{Binding Source={StaticResource Resources} Path=myText}"/>
<TextBlock x:Name="Try2">
<Binding Source="{StaticResource Resources}" Path="myText" />
</TextBlock>
</Window>
Run Code Online (Sandbox Code Playgroud)
Try1呈现标签并且不影响格式.
一些<Bold>格式化的<Bold>文本.
Try2不会编译或渲染,因为资源"myText"不是Inline类型而是字符串.
这看似简单的任务是否可能,如果可行,怎么办?
我正在从资源加载器加载文本,其中包含 '\n' 来指示新行。文本块获取此文本并显示它,但我看不到换行符?我还尝试用 Environment.NewLine 替换每个 '\n',但没有任何反应。我能做些什么?
这是一些代码:
TextBlock text = new TextBlock();
text.FontSize = 18;
var str = loader.GetString("AboutText");
//str.Replace("\n", Environment.NewLine);
text.Text = str;
text.TextAlignment = TextAlignment.Justify;
text.TextWrapping = TextWrapping.Wrap;
text.Margin = new Thickness(10, 10, 10, 10);
Run Code Online (Sandbox Code Playgroud)