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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T02:37:49+00:00 2026-05-15T02:37:49+00:00

I have one TextBox and one listbox for searching a collection of data. While

  • 0

I have one TextBox and one listbox for searching a collection of data. While searching a text inside a Listbox if that matching string is found anywhere in the list it should show in Green color with Bold.

eg. I have string collection like
“Dependency Property, Custom Property, Normal Property”. If I type in the Search Text box “prop” all the Three with “prop” (only the word Prop) should be in Bold and its color should be in green. Any idea how it can be done?.

Data inside listbox is represented using DataTemplate.

  • 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-15T02:37:49+00:00Added an answer on May 15, 2026 at 2:37 am

    I’ve created a HighlightTextBehavior that you can attach to a TextBlock within your list item templates (you’ll need to add a reference to System.Windows.Interactivity to your project). You bind the behavior to a property containing the text you want to highlight, and it does the rest.

    At the moment, it only highlights the first instance of the string. It also assumes that there is no other formatting applied to the text.

    using System.Linq;
    using System.Text;
    using System.Windows.Interactivity;
    using System.Windows.Controls;
    using System.Windows;
    using System.Windows.Documents;
    using System.Windows.Media;
    
    namespace StringHighlight
    {
        public class HighlightTextBehavior : Behavior<TextBlock>
        {
            public string HighlightedText
            {
                get { return (string)GetValue(HighlightedTextProperty); }
                set { SetValue(HighlightedTextProperty, value); }
            }
    
            // Using a DependencyProperty as the backing store for HighlightedText.  This enables animation, styling, binding, etc...
            public static readonly DependencyProperty HighlightedTextProperty =
                DependencyProperty.Register("HighlightedText", typeof(string), typeof(HighlightTextBehavior), new UIPropertyMetadata(string.Empty, HandlePropertyChanged));
    
            private static void HandlePropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
            {
                (sender as HighlightTextBehavior).HandlePropertyChanged();
            }
    
            private void HandlePropertyChanged()
            {
                if (AssociatedObject == null)
                {
                    return;
                }
    
                var allText = GetCompleteText();
    
                AssociatedObject.Inlines.Clear();
    
                var indexOfHighlightString = allText.IndexOf(HighlightedText);
    
                if (indexOfHighlightString < 0)
                {
                    AssociatedObject.Inlines.Add(allText);
                }
                else
                {
                    AssociatedObject.Inlines.Add(allText.Substring(0, indexOfHighlightString));
                    AssociatedObject.Inlines.Add(new Run() { 
                        Text = allText.Substring(indexOfHighlightString, HighlightedText.Length), 
                        Foreground = Brushes.Green,
                        FontWeight = FontWeights.Bold });
                    AssociatedObject.Inlines.Add(allText.Substring(indexOfHighlightString + HighlightedText.Length));
                }
            }
    
            private string GetCompleteText()
            {
                var allText = AssociatedObject.Inlines.OfType<Run>().Aggregate(new StringBuilder(), (sb, run) => sb.Append(run.Text), sb => sb.ToString());
                return allText;
            }
        }
    }
    

    Here’s an example of how you use it:

        <Window x:Class="StringHighlight.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
            xmlns:b="clr-namespace:StringHighlight"
            xmlns:sys="clr-namespace:System;assembly=mscorlib"
            Title="MainWindow" Height="350" Width="525">
        <Grid>
            <Grid.Resources>
                <x:Array x:Key="MyStrings" Type="{x:Type sys:String}">
                    <sys:String>This is my first string</sys:String>
                    <sys:String>Another string</sys:String>
                    <sys:String>A third string, equally imaginative</sys:String>
                </x:Array>
            </Grid.Resources>
            <Grid.RowDefinitions>
                <RowDefinition/>
                <RowDefinition/>
            </Grid.RowDefinitions>
            <TextBox x:Name="SearchText"/>
    
            <ListBox Grid.Row="1" ItemsSource="{StaticResource MyStrings}">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <TextBlock Grid.Row="1" Text="{Binding}">
                            <i:Interaction.Behaviors>
                                <b:HighlightTextBehavior HighlightedText="{Binding ElementName=SearchText, Path=Text}"/>
                            </i:Interaction.Behaviors>
                        </TextBlock>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
        </Grid>
    </Window>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Easy one for you guys. I have a textbox on top of a listbox.
I have a WPF listbox control that is declaratively bound to a textbox. The
I bind observable collection on listBox. I have data tempate on listbox item. It
I have placed one textbox.... I want to put restriction on it .. that
I have 2 textbox in my asp.net page and also have one hiddenfield in
I have one field that I need to sum lets say named items However
I have one thread that writes results into a Queue. In another thread (GUI),
I have one hibernate sequence, that generates all sequence-numbers in my app. When I
I have one text input and one button (see below). How can I use
I have a silverlight listbox that is being used as a search result box.

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.