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 8947099
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T12:39:58+00:00 2026-06-15T12:39:58+00:00

I have a textBlock to which I’ve added some text e.g. as follows: textBlock1.Inlines.Add(new

  • 0

I have a textBlock to which I’ve added some text e.g. as follows:

textBlock1.Inlines.Add(new Run("One "));
textBlock1.Inlines.Add(new Run("Two "));
textBlock1.Inlines.Add(new Run("Three "));

How can I add a click event that changes the color of the text of the inline that has been clicked?

e.g. if “One” is clicked I’d like it to have a red font; then if “Two” is clicked, I’d like “One” to be black again, and “Two” to be red, so that the color of the last word clicked is red.

I’m fairly new to programming with c# and wpf.

Thanks for your help

  • 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-06-15T12:40:01+00:00Added an answer on June 15, 2026 at 12:40 pm

    Something like this should do the trick

         public MainWindow()
        {
            InitializeComponent();
            textBlock1.Inlines.Add(new Run("One "));
            textBlock1.Inlines.Add(new Run("Two "));
            textBlock1.Inlines.Add(new Run("Three "));
        }
    
        private SolidColorBrush _blackBrush = new SolidColorBrush(Colors.Black);
        private SolidColorBrush _redBrush = new SolidColorBrush(Colors.Red);
        private Run _selectedRun;
    
        private void TextBlock_MouseDown(object sender, MouseButtonEventArgs e)
        {
            var run = e.OriginalSource as Run;
            if (run != null)
            {
                if (_selectedRun != null)
                {
                    _selectedRun.Foreground = _blackBrush;
                    if (_selectedRun == run)
                    {
                        return;
                    }
                }
                run.Foreground = _redBrush;
                _selectedRun = run;
            }
        }
    

    But you will have to handle click with “MouseDown” or “MouseUp” as Textblock does not have Click event

    To color one at a certain index this is a quick example.

    private void ColorInlineAtIndex(InlineCollection inlines, int index, Brush brush)
    {
        if (index <= inlines.Count - 1)
        {
              inlines.ElementAt(index).Foreground = brush;
        }
    }
    

    Usage:

     ColorInlineAtIndex(textBlock1.Inlines, 2, new SolidColorBrush(Colors.Blue));
    

    Find position:

    private void TextBlock_MouseDown(object sender, MouseButtonEventArgs e)
    {
        var run = e.OriginalSource as Run;
        if (run != null)
        {
            int position = (sender as TextBlock).Inlines.ToList().IndexOf(run);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a TextBlock that contains some text in some Run elements, and which
Application, have a TextBlock and two Buttons, the text is displayed TextBlock by clicking
I have following XAML: <TextBlock HorizontalAlignment=Center VerticalAlignment=Center FontSize=10 FontFamily=Arial Foreground=#414141> <Run Text={Binding LoadsCount} />
I have a Textblock which has an initial amount of text and height of
I have string which i have to display in TextBlock, my TextBlock have some
Supposing that I have two TextBlock elements, one being a label for the second,
In my view I have this: <TextBlock Text={Binding Title}/> which binds to my ViewModel's
I have a canvas control which I'm housing a shape and some text in.
I have a TextBlock which is resizing itself depending on its Text which is
I have a Textblock which Text attribute is binding to a DateTime? type data,

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.