Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 394479
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T16:21:35+00:00 2026-05-12T16:21:35+00:00

I have a TextBlock bound to a property. However, I’d like to bold certain

  • 0

I have a TextBlock bound to a property. However, I’d like to bold certain words within the text. What is the easiest way to do this? FYI, I’m new to WPF.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T16:21:35+00:00Added an answer on May 12, 2026 at 4:21 pm

    Bolding individual words involves actually creating more inline elements, so you can’t just bind a string to the TextBlock’s Text and do this.

    What I’ve done for this in the past is created a subclass of TextBlock which has a custom property that I bind to. When this property is bound I clear the Inlines of the base TextBlock and then use an algorithm that parses the new string value creating either plain Runs, Bolds, Hyperlinks etc.

    Here’s some sample code which I wrote for my experimental Twitter client which detects URLs, emails and @ pattern and creates hyperlinks for them. Regular text is inlined as normal runs:

    [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(@"(?<url>[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_\-\.\~\%\+\?\=\&\;\|/]*)?)|(?<emailAddress>[^\s]+@[a-zA-Z0-9]+([\-\.]{1}[a-zA-Z0-9]+)*\.[a-zA-Z]{2,5})|(?<toTwitterScreenName>\@[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<Inline> 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("&gt;", ">").Replace("&lt;", "<");
        }
    
        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
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a TextBlock bound to a string. I want the string to be
I have created a datagrid with columns bound to an observable collection. All works
I have a little issue. I have a a ListBox that is bound to
I have an ItemsControl with a DataTemplate that is bound to an ObservableCollection of
I am trying to scroll text across the screen which is working well. Update
I have recently started using the MVVM-Light toolkit and I am stuck on the
I have two UserControls (uc1 and uc2) loading into a third UserControl (shell). Shell
Ok, So I have tried to implement a coverflow found on codeplex http://silverlightcoverflow.codeplex.com/ I
Hello, I have 3 Buttons add,delete,open as RelayCommands in my DocumentViewModel . Below you
Hallo Stackoverflow fellas! In my recent wpf application building I ancountered a strange behaviour:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.