How Do I Prevent a Column in a Grid From Gaining Focus?

Let’s say you have some columns in your LightSwitch grid which are set to use a view-only control (Label, Money Viewer, Phone Viewer, etc.). You might prefer for the TAB key to skip over those fields when the user is entering data in the grid. If you want to prevent all viewer fields from gaining focus, you can use this method.

Add the following using statements to your screen’s code:

using Microsoft.LightSwitch.Presentation.Implementation.Controls;
using System.Windows.Controls;

And add the following code to your screen’s Created method:

this.FindControl("NameOfMyGrid").ControlAvailable += (s, e) =>
{
  var grid = e.Control as DataGrid;
  foreach (var column in grid.Columns)
  {
    var ci = column.Header as ContentItemWrapperForColumnHeader;
    column.IsReadOnly = ci.ContentItem.View.IsViewer;
  }
}