UWP:为文本块中的特定单词添加样式
UWP: Add styling to specific words in textblock
我的资源文件中有几个字符串需要翻译。这些字符串将连接成一个句子并显示在我的应用程序中的 TextBlock
中,我想在 TextBlock
中将一些字符串加粗。
例如
string a = "This {0} a {1} string.";
string b = "is";
string c = "formatted";
string output = string.Format(a, b, c);
我希望输出为:This is a formatted string.
欢迎任何提示和提示。
在这里您可以找到很多关于设置文本块样式的信息:http://www.wpf-tutorial.com/basic-controls/the-textblock-control-inline-formatting/
我建议使用 span 方法来解决你的问题,因为你想在程序代码中工作
祝你好运
在 TextBlock
中使用 <Run />
为特定字词添加额外的样式。
<TextBlock>
This
<Run FontWeight="Bold">is</Run>
a
<Run FontWeight="Bold">formatted</Run>
string.
</TextBlock>
更新
这里有一个简单的例子可以给你一个想法。我创建了一个名为 TextControl
的 UserControl
,其中 XAML 仅包含此 -
<TextBlock x:Name="MyTextBlock" />
在其代码隐藏中,我定义了三个依赖属性。一个是取完整的资源字符串,另外两个是传入{0}
和{1}
。这个想法是将整个字符串拆分为 space,然后将 {x}
替换为输入属性并将它们设为粗体。
public string ResourceString
{
get => (string)GetValue(ResourceStringProperty);
set => SetValue(ResourceStringProperty, value);
}
public static readonly DependencyProperty ResourceStringProperty = DependencyProperty.Register(
"ResourceString", typeof(string), typeof(TextControl), new PropertyMetadata(default(string), (s, e) =>
{
var self = (TextControl)s;
var full = e.NewValue.ToString();
foreach (var seg in full.Split(' '))
{
var run = new Run();
if (seg.Contains("{0}"))
{
run.Text = self.Input0;
run.FontWeight = FontWeights.Bold;
}
else if (seg.Contains("{1}"))
{
run.Text = self.Input1;
run.FontWeight = FontWeights.Bold;
}
else
{
run.Text = seg;
}
run.Text += " ";
self.MyTextBlock.Inlines.Add(run);
}
}));
public string Input0
{
get => (string)GetValue(Input0Property);
set => SetValue(Input0Property, value);
}
public static readonly DependencyProperty Input0Property = DependencyProperty.Register(
"Input0", typeof(string), typeof(TextControl), new PropertyMetadata(default(string)));
public string Input1
{
get => (string)GetValue(Input1Property);
set => SetValue(Input1Property, value);
}
public static readonly DependencyProperty Input1Property = DependencyProperty.Register(
"Input1", typeof(string), typeof(TextControl), new PropertyMetadata(default(string)));
那你就可以这样用了-
<local:TextControl Input0="is" Input1="formatted" ResourceString="This {0} indeed a {1} string." />
然后你得到 -
我的资源文件中有几个字符串需要翻译。这些字符串将连接成一个句子并显示在我的应用程序中的 TextBlock
中,我想在 TextBlock
中将一些字符串加粗。
例如
string a = "This {0} a {1} string.";
string b = "is";
string c = "formatted";
string output = string.Format(a, b, c);
我希望输出为:This is a formatted string.
欢迎任何提示和提示。
在这里您可以找到很多关于设置文本块样式的信息:http://www.wpf-tutorial.com/basic-controls/the-textblock-control-inline-formatting/
我建议使用 span 方法来解决你的问题,因为你想在程序代码中工作
祝你好运
在 TextBlock
中使用 <Run />
为特定字词添加额外的样式。
<TextBlock>
This
<Run FontWeight="Bold">is</Run>
a
<Run FontWeight="Bold">formatted</Run>
string.
</TextBlock>
更新
这里有一个简单的例子可以给你一个想法。我创建了一个名为 TextControl
的 UserControl
,其中 XAML 仅包含此 -
<TextBlock x:Name="MyTextBlock" />
在其代码隐藏中,我定义了三个依赖属性。一个是取完整的资源字符串,另外两个是传入{0}
和{1}
。这个想法是将整个字符串拆分为 space,然后将 {x}
替换为输入属性并将它们设为粗体。
public string ResourceString
{
get => (string)GetValue(ResourceStringProperty);
set => SetValue(ResourceStringProperty, value);
}
public static readonly DependencyProperty ResourceStringProperty = DependencyProperty.Register(
"ResourceString", typeof(string), typeof(TextControl), new PropertyMetadata(default(string), (s, e) =>
{
var self = (TextControl)s;
var full = e.NewValue.ToString();
foreach (var seg in full.Split(' '))
{
var run = new Run();
if (seg.Contains("{0}"))
{
run.Text = self.Input0;
run.FontWeight = FontWeights.Bold;
}
else if (seg.Contains("{1}"))
{
run.Text = self.Input1;
run.FontWeight = FontWeights.Bold;
}
else
{
run.Text = seg;
}
run.Text += " ";
self.MyTextBlock.Inlines.Add(run);
}
}));
public string Input0
{
get => (string)GetValue(Input0Property);
set => SetValue(Input0Property, value);
}
public static readonly DependencyProperty Input0Property = DependencyProperty.Register(
"Input0", typeof(string), typeof(TextControl), new PropertyMetadata(default(string)));
public string Input1
{
get => (string)GetValue(Input1Property);
set => SetValue(Input1Property, value);
}
public static readonly DependencyProperty Input1Property = DependencyProperty.Register(
"Input1", typeof(string), typeof(TextControl), new PropertyMetadata(default(string)));
那你就可以这样用了-
<local:TextControl Input0="is" Input1="formatted" ResourceString="This {0} indeed a {1} string." />
然后你得到 -