简体   繁体   中英

Hashtags in Binding TextBlock C#

I have a listview in my app (C# - UWP) and in my DataTemplate i have a TextBlock that it Binding a text.

this is my code:

 <TextBlock Text="{Binding Caption}" FontSize="11"/>

Now, how can i coloring all hashtags in the text? And clickable? Note: all captions that binding this TextBlock is variable. Like:

This is test #message for testing

Or

I like #German and #Russian language

I want change color #message, #German and #Russian and clickable feature in TextBlock

One option is to use a rich text box. Rich text box can render HTML like tags.

so you can have text like

<p> I am following the <a>#Russian-Language</a> <a>#azure</a> tutorials. </P>

Then anchor tags can have targets and they will be clickable. OR you can call a method on this hyperlink click.

Hope this helps you.

I made a control few months back called HashHandleTextBlock . The core concept of this is based on MarkdownTextBlock of UWP Community Toolkit.

Below is how you use controls.

<UnwantedControls:HashHandleTextBlock Text="{Binding ElementName=InputText, Path=Text}" 
                                      LinkForeground="DarkGray"
                                      HashPrefix="https://twitter.com/hashtag/" 
                                      HandlePrefix="https://twitter.com/" />

You can also download the source from Github and modify control for your requirement.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM