Click here to Skip to main content
Click here to Skip to main content

Row Highlighting in WPF Grids

, 14 May 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

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

Comments and Discussions

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