Solution 1 : Determining which datagrid has focus

 

You have three DataGrid controls named DataGrid1, DataGrid2, DataGrid3 and a button named ExportToExcelButton on a Silverlight XAML page. When you click the ExportToExcelButton, you want to export data to an Excel file from the DataGrid that has focus. To implement this, you need to determine which DataGrid has focus.

Silverlight DataGrid control does not provide a property such as HasFocus directly. But you can achieve this feature by utilizing the GotFocus event of DataGrid control.

1.       In the XAML code behind, define an enum and a private member variable.

2.       In the XAML page Loaded event, set a default DataGrid control to receive focus. For example, the following code set the DataGrid1 to receive focus by default.

3.       In  each GotFocus event handler of the three DataGrids, set value to the private member variable.

4.       In the ExportToExcelButton_Click event, determine which DataGrid has focus based on value of the private member variable.

 

 

2 Comments

  • Why to use the enum? You can have one shared handler for all the grids

    private void DataGrid_GotFocus(object sender, ...)
    {
    dataGridToExport = (sender as DataGrid);
    }

    and then easily process it

    private void ExportToExcelButton_Click(...)
    {
    if(dataGridToExport != null)
    {
    // Export here
    }
    }

  • And how does that mean? I do not understand anything.

Comments have been disabled for this content.