WPF TextBlock highlight certain parts based on search condition

后端 未结 9 1145
遇见更好的自我
遇见更好的自我 2020-12-01 08:42

I have TextBlock that has Inlines dynamicly added to it (basically bunch of Run objects that are either italic or bold).

In my application I have search function.

9条回答
  •  Happy的楠姐
    2020-12-01 08:52

    By strange coincidence, I have recently written an article that solves the very same problem. It is a custom control that has the same properties as a TextBlock (so you can swap is out for a TextBlock wherever you need it), and it has an extra Property that you can bind to called HighLightText, and wherever the value of HighLightText is found in the main Text property (case insensitive), it is highlighted.

    It was a fairly straight-forward control to create, and you can find the article here:

    WPF TextBlock With Search String Matching

    And the full code as a solution here:

    SearchMatchTextblock(GitHub)

提交回复
热议问题