Click here to Skip to main content
13,298,655 members (59,771 online)
Click here to Skip to main content
Add your own
alternative version


2 bookmarked
Posted 14 May 2014

Row Highlighting in WPF Grids

, 14 May 2014
Rate this:
Please Sign up or sign in to vote.
Short example of highlighting a grid row on textbox got focus


For ease of use, it may be of interest to highlight a row in a table. The table is created using a grid, filled with borders (spanning the entire row), labels and textboxes. On getting focus of any of the textboxes, the border-element in that row is chosen and highlighted. On losing focus, it is dimmed again.


The table is created on runtime.
Handlers are added to handle focus events of the Textboxes using one common procedure.
The handler then determines the row and highlights the border element by looking for the first element in that row.

Using the Code

The main task was to find the border element in any given row.

This is done using the procedure setRowColor below.

''' <summary>
''' Helper for coloring the first element of a given row in column 0 (our border)
''' </summary>
''' <param name="row">row to highlight</param>
''' <param name="myBrush">brush with defined background</param>
''' <remarks></remarks>
Private Sub setRowColor(ByVal row As Integer, ByVal myBrush As Brush)
    Dim column As Integer = 0
    Dim el As Border = Me.Grid1.Children.Cast(Of UIElement)().First( _
            Function(uiEl) Grid.GetRow(uiEl) = row AndAlso Grid.GetColumn(uiEl) = column)
    el.Background = myBrush
End Sub

Points of Interest

Tables in WPF may be defined programmatically with some knowledge. The aim of row highlighting was done using borders with defined background.


  • 14.5.2014: First version


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Markus Wallner-Novak
Engineer WallnerMild TimberEngineering Software
Austria Austria
No Biography provided

You may also be interested in...

Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.171207.1 | Last Updated 14 May 2014
Article Copyright 2014 by Markus Wallner-Novak
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid