On tabbing into the second cell of the XamDataGrid i want that the element which is in the cell should get focus and not the outer cell. I am using ValueEditor in the style. I also tried CellValuePresenter but still the focus remains on the outer cell and not on the element.
I have developed a sample application. Attached is the same. Please have a look and suggest what can be done.
I have been investigating into the sample project you have provided, and I am under the impression that in the sample project, you are trying to have the Button be focused. If my impression on this is correct, I have a couple of recommendations for you on this matter.
My best recommendation on this is to utilize a TemplateField and a DisplayTemplate for that TemplateField rather than re-templating the ValueEditor element for your Field to place a Button inside. A TemplateField was built so that you can effectively put any element you want inside without needing to re-template elements of the grid. I have tried this using the sample project and the Button focuses automatically using this method. You can read about this method at this documentation article.
I am attaching a sample project to demonstrate this, along with a workaround using the ValueEditor re-templating using the CellActivated event of the grid, if for some reason you are looking to continue with that instead of using the TemplateField. I would highly recommend using a TemplateField for this, though.
Please let me know if you have any other questions or concerns on this matter.
Thanks Andrew for your reply.
Your solution of Template does work but one other issue which i am facing is still not solved. I can post another question in the forum for this but i request you to look into it in this here itself so that i don't have to explain all the things again to anyone I have marked your previous reply as verified.
I want to show the message on click of the button on the single click. That is if the row in which we are clicking the button is not in focus, then on the single click it should show the message. What i mean to say is that click of button should execute on the single click.
Now there is a property called "CellClickAction" present in TemplateField.Setttings. When it is set to "SelectCell" then the above problem gets addressed but then the focus on tab does not go to the element. When it is set to "EnterEditModeIfAllowed" then the focus goes to the element on tab but single click stops working.
Is this the default behavior of XamDataGrid? Below is the link which has some code i explained above as commented.
I believe that what is happening with the new issue in this case is that when your CellClickAction is in “EnterEditModeIfAllowed” property is set, the TemplateField will try to look for an EditTemplate, and if it doesn’t find one, it will simply select the cell on click, and I believe this cell selection is essentially “eating” the Button click in this case. The “SelectCell” option is selecting the cell, but is allowing the click to get through to the Button. I do believe this is likely the default behavior of the XamDataGrid, as the TemplateField behaves a little bit differently than the other fields in the grid.
In order to focus the Button with the “SelectCell” mode, I would simply recommend utilizing the CellActivated method that was provided in the previously attached project, in which you can programmatically focus the button within the cell on activation.
Thanks for the reply.
It's not just TemplateField for which this issue is happening but it is applicable to any "Field" used in xamdatagrid. As you said that this is default behavior of XamDatagrid, there is hardly anything else we can think of to do.
Yes I can use CellActivated but then I have several links in the several grids. Not only it would be a costly affair to use this event but also it would be difficult to write a generic code for them (without using Field Names).
Can we still think of any other approach or solution so that both click as well as tab focus works together?
I have been investigating into this behavior a bit further, and I have found that it is the CellValuePresenter, and effectively the ValueEditor that exists inside that is “eating” the event and so the Click event is never happening in this case. In the case of the TemplateField, the ValueEditor derivation that is inside of the cell is a TemplateEditor.
I have been doing some experimentation, and I would like to recommend another way in this case. I have been trying a couple of things using CellValuePresenter styles, and in setting the ContentTemplate to a new DataTemplate containing a Button (much like the one in the TemplateField) and then setting it to be the CellValuePresenterStyle of a Field, I am able to see that the Button gets focus and can be clicked normally. Something about this appears to be getting around the CellValuePresenter eating the event, although I’m not entirely sure what at the moment.
I am attaching a modified version of the sample project you sent to demonstrate. I hope this helps you.