Silverlight 4: Applying Style to the Selected DataGrid Cell during Edit Operation

Posted by: Mahesh Sabnis , on 8/28/2011, in Category Silverlight 2, 3, 4 and 5
Views: 82795
Abstract: The Silverlight DataGrid has lots of events for User Interaction. One of the event provided is PreparingCellForEdit, which is raised when a cell in the DataGrid column enters Editing mode. During this event, the DataGridCell is templated as the TextBox control and we can then manipulate data within the textbox. In this article, I have used the ‘PreparingCellForEdit’ event to apply a style on the DataGrid Cell

Silverlight DataGrid is a good control for data display and manipulation. This control provides various features to the developer for UI representation and customization. The DataGrid has lots of events for User Interaction. One of the event provided is PreparingCellForEdit, which is raised when a cell in the DataGrid column enters Editing mode. During this event, the DataGridCell is templated as the TextBox control and we can then manipulate data within the textbox.

In this article, I have used the ‘PreparingCellForEdit’ event to apply a style on the DataGrid Cell. First my XAML code defines the Style for the TextBox and when the cell is clicked, this style is applied on it.

Step 1: Open VS2010 and create a Silverlight application, name it as ‘SL4_DataGridCell’. In the MainPage.Xaml.cs add the following classes which acts as DataClasses.

image

Step 2: In the MainPage.xaml, add a DataGrid and define a Style targeting the TextBox element as below:

silverlight-datagrid-cell-style

Step 3: In the MainPage.Xaml.cs, add the following code in the Loaded event and apply the Style programmatically to the DataGridCell using ‘PreparingCellForEdit’ event as below:

Silverlight DataGrid Edit Event

Step 4: Run the application and click on the cell, the result will be as shown below:

image

The entire source code of this article can be downloaded over here

Give me a +1 if you think it was a good article. Thanks!
Recommended Articles


Page copy protected against web site content infringement by Copyscape


User Feedback
Comment posted by test on Thursday, October 18, 2012 2:28 AM
test comment

Post your comment
Name:  
E-mail: (Will not be displayed)
Comment:
Insert Cancel