locked
How to highlight search-word in bold all the returned result

    Question

  • I am using SQLite and SQLite-net to search a Keyword in the tables and bind the result to textboxes inside StackPanel in the ListView item.

    Say, the keyword is "Fun", I get all the rows in the tables which contain the word  such as below

    I love fun stuff

    Fun is good

    I get fun, hellava fun, load of fun

    Problem:

    How to highlight or make the word fun "bold" in above results in the ListView

    Wednesday, November 26, 2014 1:31 PM

Answers

  • Hi FireDance,

    That could be a complicate scenario, I have one idea to implement this.

    TextBox cannot do this, but you can use TextBlock instead.

    Here I have some code for you:

            public MainPage()
            {
                this.InitializeComponent();
                TextBlock a = words("fun", "I get fun, hellava fun, load of fun");
                gd.Children.Add(a);
            }
    
    
    
            public TextBlock words(string separator, string content)
            {
                TextBlock myBlock = new TextBlock();
    
                string[] stringSeparators = new string[] { separator };
                var stringlist = content.Split(stringSeparators, StringSplitOptions.None);
    
                foreach (string words in stringlist)
                {
                    var block = new Run();
                    block.Text = words;
                    myBlock.Inlines.Add(block);
    
                    if (stringlist[stringlist.Length - 1] != words)
                    {
                        var block1 = new Run();
                        block1.Text = separator;
                        block1.FontWeight = FontWeights.Bold;
                        myBlock.Inlines.Add(block1);
                    }
                }
    
                return myBlock;
            }

    The result:

    Looks better?

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Thursday, November 27, 2014 2:25 AM
    Moderator

All replies

  • Hi FireDance,

    That could be a complicate scenario, I have one idea to implement this.

    TextBox cannot do this, but you can use TextBlock instead.

    Here I have some code for you:

            public MainPage()
            {
                this.InitializeComponent();
                TextBlock a = words("fun", "I get fun, hellava fun, load of fun");
                gd.Children.Add(a);
            }
    
    
    
            public TextBlock words(string separator, string content)
            {
                TextBlock myBlock = new TextBlock();
    
                string[] stringSeparators = new string[] { separator };
                var stringlist = content.Split(stringSeparators, StringSplitOptions.None);
    
                foreach (string words in stringlist)
                {
                    var block = new Run();
                    block.Text = words;
                    myBlock.Inlines.Add(block);
    
                    if (stringlist[stringlist.Length - 1] != words)
                    {
                        var block1 = new Run();
                        block1.Text = separator;
                        block1.FontWeight = FontWeights.Bold;
                        myBlock.Inlines.Add(block1);
                    }
                }
    
                return myBlock;
            }

    The result:

    Looks better?

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Thursday, November 27, 2014 2:25 AM
    Moderator
  • Hi Jamles,

    Thank you so much for the solution. I will use your solution concept for my problem. Will update here asap.

    Thursday, November 27, 2014 5:57 AM