Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Row Highlighting in WPF Grids

0.00/5 (No votes)
14 May 2014 1  
Short example of highlighting a grid row on textbox got focus

Introduction

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.

Background

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.

History

  • 14.5.2014: First version

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here