Click here to Skip to main content
15,881,248 members
Articles / Desktop Programming / WPF
Tip/Trick

Row Highlighting in WPF Grids

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
14 May 2014CPOL 11.5K   184   2  
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.

Image 1

Using the Code

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

This is done using the procedure setRowColor below.

VB.NET
''' <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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Engineer WallnerMild TimberEngineering Software
Austria Austria
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --