DataGrid highlighting behavior

Yet another attached behavior for Silverlight 3 that may be quite handy - one that dynamically highlights text inside DataGrid. It does not require changes in DataGrid itself - just attach the DataGridHighlightingBehavior to your DataGrid and tell it the substring you want to highlight, it'll do the rest.

Features:

  • Highlighting text can be dynamically changed
  • Works fine with paging
  • Supports case-sensitive or case-insensitive highlighting

demo.png

Notes on the implementation:

  • The implementation is based on great Allen Chen's DataGrid highlighting sample. However, this sample did the highlighting for one very specific case only, and only the first found substring was selected. DataGridHighlightingBehavior generalizes this idea and makes it reusable by separation into attached behavior.
  • Due to known limitations in DependencyObject binding (which will be fixed in Silverlight 4) highlighting text could be set only through binding (using HighlightingTextBinding property). The underlying code uses excellent BindingListener class from Expression Blend Samples library.
  • For now, DataGridHighlightingBehavior supports only cells containing TextBlock. This behavior could be easily extended by changing cell contents iteration code.

NOTE: This article is outdated after release of Silverlight 4. Much simpler version of the same behavior you can find here.

AttachmentSize
datagridhighlightingbehaviordemo.zip31.69 KB

Comments

Well done!

Thanks Alexandr! Great work, just what I'm looking for. Saved me hours!

Thank you

Thank you, glad this code was helpful.