TextBlock Windows Phone 8.1 中的文本格式

编程入门 行业动态 更新时间:2024-10-25 22:25:13
本文介绍了TextBlock Windows Phone 8.1 中的文本格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我的 ViewModel 中有一个字符串列表,格式如下:

I have a list of strings in my ViewModel with the following format:

 This is an <b>example.<b>

我想在我的视图中使用某种文本控件,通过 DataBinding* 显示格式化文本,如下所示:

I want to have some kind of text control in my view that will display the formatted text via DataBinding*, like this:

这是一个示例.

我没有找到任何可以像这样运行的内置控件.

I don't find any built-in control that can behave like this.

有人知道怎么处理吗?

推荐答案

您可以使用 运行:

<TextBlock FontSize="30">
    <Run>This is an</Run>
    <Run FontWeight="Bold" Text=" example"/>
</TextBlock>

为此,您必须解析字符串,选择粗体部分,并在后面的代码中定义内容.一个非常简单的示例如下所示:

For this you will have to parse your strings, select bold sections, and define content in the code behind. A very simple example can look like this:

string example = @"This is an <b>example.</b>";
var str = example.Split(new string[] { "<b>", "</b>" }, StringSplitOptions.None);
for (int i = 0; i < str.Length; i++)
    myTextBlock.Inlines.Add(new Run { Text = str[i], FontWeight = i % 2 == 1 ? FontWeights.Bold : FontWeights.Normal });

<小时>

编辑 - 与绑定一起使用

如果你想把上面的过程和 Binding 一起使用,那就没那么简单了——TextBlock.Inlines 不是一个 DependencyProperty,所以我们不能使用它.然而,有一种方法可以做到这一点 - 您需要以某种方式扩展您的 TextBlock - 这是另一个陷阱 - 它是 密封 类,因此没有继承.在这种情况下,我们将不得不使用另一个类(这里也是一个很好的例子):

If you want to use the above procedure with Binding, then it's not so simple - TextBlock.Inlines is not a DependencyProperty, so we cannot use it. Nevertheless there is a way to do it - you need to extend somehow your TextBlock - here is another pitfall - it's sealed class so no inheritance. In this case we will have to use another class (here is also a good example):

public static class TextBlockExtension
{
    public static string GetFormattedText(DependencyObject obj)
    { return (string)obj.GetValue(FormattedTextProperty); }

    public static void SetFormattedText(DependencyObject obj, string value)
    { obj.SetValue(FormattedTextProperty, value); }

    public static readonly DependencyProperty FormattedTextProperty =
        DependencyProperty.Register("FormattedText", typeof(string), typeof(TextBlockExtension),
        new PropertyMetadata(string.Empty, (sender, e) =>
        {
            string text = e.NewValue as string;
            var textBl = sender as TextBlock;
            if (textBl != null)
            {
                textBl.Inlines.Clear();
                var str = text.Split(new string[] { "<b>", "</b>" }, StringSplitOptions.None);
                for (int i = 0; i < str.Length; i++)
                    textBl.Inlines.Add(new Run { Text = str[i], FontWeight = i % 2 == 1 ? FontWeights.Bold : FontWeights.Normal });
            }
        }));
}

然后你可以像这样在 xaml 中使用它:

Then you can use it in xaml like this:

<TextBlock local:TextBlockExtension.FormattedText="{Binding MyText}"/>

这篇关于TextBlock Windows Phone 8.1 中的文本格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-25 13:41:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1117567.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文本   格式   Windows   TextBlock   Phone

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!