This is a migrated thread and some comments may be shown as answers.

[Solved] Get handle on generated UIElements from a cell selection?

5 Answers 110 Views
Grid for XAML
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Roger
Top achievements
Rank 1
Roger asked on 08 Sep 2013, 10:29 PM
I am using a custom CellContentTemplate to use custom XAML for each of my cells. Each of my cells has a textbox which I would like an event handler on so that I know when that field has gained focus. I had tried to do this by getting a collection of all of the textbox descendants of the grid, however that doesn't work well since the grid is virtualized and may only contain a few textboxes, not every one in the grid.

I am now trying a different route by looking at the SelectionChanged event on the grid itself, and then trying to get a handle on the textbox contained within the cell. Is there any way to go from a DataGridCellInfo object to the UIElements/controls that make up that cell so that I can get a handle on the textbox within that cell?

Is there another way to do this such as getting notified when new cells are created vs recycled in the virtualization so that I could add/remove event handlers there?

Thanks for any suggestions!

5 Answers, 1 is accepted

Sort by
0
Ivaylo Gergov
Telerik team
answered on 10 Sep 2013, 03:50 PM
Hi Roger,

Thank you for contacting us.

In order to add event handlers for each cell's element, you need to hook the event handler in the DataTemplate of the desired column. For example:
<telerikGrid:DataGridTemplateColumn.CellContentTemplate>
    <DataTemplate>
            <TextBlock GotFocus="TextBlock_GotFocus"/>       
    </DataTemplate>
</telerikGrid:DataGridTemplateColumn.CellContentTemplate>


Let me know if this works for you.

 

Regards,
Ivaylo Gergov
Telerik

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Roger
Top achievements
Rank 1
answered on 11 Sep 2013, 01:06 PM
Unfortunately that causes an exception. I've made sure the event is properly formatted by generating the method from a XAML definition, and have ensured it is in the same class as where the DataTemplate is created.

{Windows.UI.Xaml.UnhandledExceptionEventArgs}
    Exception: {"Error HRESULT E_FAIL has been returned from a call to a COM component."}
    Handled: false
    Message: "System.Runtime.InteropServices.COMException\r\n   at Windows.UI.Xaml.DataTemplate.LoadContent()\r\n   at Telerik.UI.Xaml.Controls.Grid.DataGridTemplateColumn.CreateContainer(Object rowItem)\r\n   at Telerik.UI.Xaml.Controls.Grid.View.XamlContentLayer.GenerateCellContainer(Object rowItem, DataGridColumn definition)\r\n   at Telerik.UI.Xaml.Controls.Grid.XamlGridCellGenerator.Telerik.UI.Xaml.Controls.Grid.IUIContainerGenerator<Telerik.UI.Xaml.Controls.Grid.GridCellModel,Telerik.UI.Xaml.Controls.Grid.CellGenerationContext>.GenerateContainerForItem(CellGenerationContext info, Object containerType)\r\n   at Telerik.UI.Xaml.Controls.Grid.CellModelGenerator.GenerateContainerForItem(CellGenerationContext context, Object containerType)\r\n   at Telerik.UI.Xaml.Controls.Grid.ItemModelGenerator`2.GenerateContainer(K context)\r\n   at Telerik.UI.Xaml.Controls.Grid.CellsController`1.GetCellDecorator(IItemInfoNode parentRow, ItemInfo columnItemInfo, Int32 rowLine, Int32 columnLine)\r\n   at Telerik.UI.Xaml.Con
trols.Grid.CellsController`1.GenerateCellsForRow(IItemInfoNode rowDecorator, Int32 rowSlot)\r\n   at Telerik.UI.Xaml.Controls.Grid.GridModel.GenerateCellsForReadOnlyRow(Int32 rowSlot, Double largestRowElementWidth, IItemInfoNode rowDecorator)\r\n   at Telerik.UI.Xaml.Controls.Grid.GridModel.Telerik.UI.Xaml.Controls.Grid.ITable.GenerateCellsForRow(Int32 rowSlot, Double largestRowElementHeight, IItemInfoNode rowDecorator)\r\n   at Telerik.UI.Xaml.Controls.Grid.NodePool`2.GenerateCellsForLine(Int32 slot, Double largestLength, T lastElement)\r\n   at Telerik.UI.Xaml.Controls.Grid.NodePool`2.GenerateContainer(IList`1 itemInfos)\r\n   at Telerik.UI.Xaml.Controls.Grid.NodePool`2.OnMeasure(RadSize availableSize, Double offset)\r\n   at Telerik.UI.Xaml.Controls.Grid.GridModel.MeasureCells(RadSize newAvailableSize)\r\n   at Telerik.UI.Xaml.Controls.Grid.RadDataGrid.OnCellsPanelMeasure(RadSize newAvailableSize)\r\n   at Telerik.UI.Xaml.Controls.Grid.Primitives.DataGridCellsPanel.MeasureOverride(Size availableSize)   at W
indows.UI.Xaml.UIElement.Measure(Size availableSize)\r\n   at Telerik.UI.Xaml.Controls.Grid.Primitives.DataGridRootPanel.MeasureOverride(Size availableSize)"

Is there a way to go from a selected cell to it's contained UIElements instead? 
0
Ivaylo Gergov
Telerik team
answered on 11 Sep 2013, 02:52 PM
Hello Roger,

Unfortunately, it would not be possible to access the UI element that represents the selected cell. The main reason is that the RadDataGrid uses UI virtualization.
Could you please send us a sample project that reproduces the exception? Thus, we will be able to further investigate.

I am looking forward to your reply.

 

Regards,
Ivaylo Gergov
Telerik

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Roger
Top achievements
Rank 1
answered on 13 Sep 2013, 02:08 AM
I was able to get around the need for event handlers by using messaging. I am still curious though on tying cells to contained fields. 

In a simple data template you might have a textbox as well as other things like a label, etc. in each cell. Is there no way then for the selection of a cell to in turn put the focus on the contained textbox?
0
Rosy Topchiyska
Telerik team
answered on 16 Sep 2013, 02:45 PM
Hi Roger,

I have to admit that there is a bug with the RadDataGrid - when you select a cell from a templated column, the first focusable element of the grid gets the focus instead of the content of the selected cell. I have redirected this issue to our developers and they will do their best to fix it for the official release(planned for mid-October). I have also updated your Telerik points as a token of gratitude for bringing this issue to our attention.

Feel free to contact us if you have any other questions or ideas.

Regards,
Rositsa Topchiyska
Telerik

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

Tags
Grid for XAML
Asked by
Roger
Top achievements
Rank 1
Answers by
Ivaylo Gergov
Telerik team
Roger
Top achievements
Rank 1
Rosy Topchiyska
Telerik team
Share this question
or