c#加粗代码_c# – 如何加粗绑定TextBlock的部分?

本文介绍了如何在C#中创建一个TextBlock子类,用于处理绑定的字符串,并能实现部分文字加粗以及检测和转换URL、电子邮件和@提及为超链接。通过使用正则表达式匹配和自定义依赖属性,动态生成内联元素来展示内容。
摘要由CSDN通过智能技术生成

粗体化单个单词涉及实际创建更多内联元素,因此您不能将字符串绑定到TextBlock的Text并执行此操作.

我过去为此所做的是创建了一个TextBlock的子类,它有一个我绑定的自定义属性.当绑定此属性时,我清除基本TextBlock的内联,然后使用解析新字符串值的算法创建普通运行,粗体,超链接等.

这是我为实验性Twitter客户端编写的一些示例代码,它检测URL,电子邮件和@模式,并为它们创建超链接.常规文本内联为正常运行:

[ContentProperty("StatusText")]

public sealed class StatusTextBlock : TextBlock

{

#region Fields

public static readonly DependencyProperty StatusTextProperty = DependencyProperty.Register(

"StatusText",

typeof(string),

typeof(StatusTextBlock),

new FrameworkPropertyMetadata(string.Empty, FrameworkPropertyMetadataOptions.None, StatusTextBlock.StatusTextPropertyChangedCallback, null));

private static readonly Regex UriMatchingRegex = new Regex(@"(?[a-zA-Z]+:\/\/[a-zA-Z0-9]+([\-\.]{1}[a-zA-Z0-9]+)*\.[a-zA-Z]{2,5}(:[0-9]{1,5})?([a-zA-Z0-9_\-\.\~\%\+\?\=\&\;\|/]*)?)|(?[^\s]+@[a-zA-Z0-9]+([\-\.]{1}[a-zA-Z0-9]+)*\.[a-zA-Z]{2,5})|(?\@[a-zA-Z0-9\-_]+)", RegexOptions.Compiled);

#endregion

#region Constructors

public StatusTextBlock()

{

}

#endregion

#region Type specific properties

public string StatusText

{

get

{

return (string)this.GetValue(StatusTextBlock.StatusTextProperty);

}

set

{

this.SetValue(StatusTextBlock.StatusTextProperty, value);

}

}

#endregion

#region Helper methods

internal static IEnumerable GenerateInlinesFromRawEntryText(string entryText)

{

int startIndex = 0;

Match match = StatusTextBlock.UriMatchingRegex.Match(entryText);

while(match.Success)

{

if(startIndex != match.Index)

{

yield return new Run(StatusTextBlock.DecodeStatusEntryText(entryText.Substring(startIndex, match.Index - startIndex)));

}

Hyperlink hyperLink = new Hyperlink(new Run(match.Value));

string uri = match.Value;

if(match.Groups["emailAddress"].Success)

{

uri = "mailto:" + uri;

}

else if(match.Groups["toTwitterScreenName"].Success)

{

uri = "http://twitter.com/" + uri.Substring(1);

}

hyperLink.NavigateUri = new Uri(uri);

yield return hyperLink;

startIndex = match.Index + match.Length;

match = match.NextMatch();

}

if(startIndex != entryText.Length)

{

yield return new Run(StatusTextBlock.DecodeStatusEntryText(entryText.Substring(startIndex)));

}

}

internal static string DecodeStatusEntryText(string text)

{

return text.Replace(">", ">").Replace("<", "

}

private static void StatusTextPropertyChangedCallback(DependencyObject target, DependencyPropertyChangedEventArgs eventArgs)

{

StatusTextBlock targetStatusEntryTextBlock = (StatusTextBlock)target;

targetStatusEntryTextBlock.Inlines.Clear();

string newValue = eventArgs.NewValue as string;

if(newValue != null)

{

targetStatusEntryTextBlock.Inlines.AddRange(StatusTextBlock.GenerateInlinesFromRawEntryText(newValue));

}

}

#endregion

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值