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

Multiple textboxes/textblocks in a cell, chooses right one on edit.

3 Answers 83 Views
GridView
This is a migrated thread and some comments may be shown as answers.
David
Top achievements
Rank 1
David asked on 06 Dec 2012, 03:54 PM
I have a GridViewDataColumn with a CellTemplate that contains an ItemsControl which is templated for TextBlocks. The CellEditTemplate has a similar setup only with TextBoxes. Essentially I have several TextBlocks/TextBoxes per cell. What I want is when the user double clicks on a TextBlock the grid goes into editing mode and selects the corresponding TextBox by index. If I double click the third TextBlock in the cell then I want the third TextBox to start editing.

The default behavior is the grid selects the first TextBox upon entering edit mode. I have tried a couple of code behind solutions based on moving through the visual tree when PreviewMouseDoubleClick happens. But when I force the grid to enter edit mode the TextBoxes are not part of the visual tree as I would expect them to be. 

I would prefer a pure XAML solution but do not know how that would be done.

3 Answers, 1 is accepted

Sort by
0
Pavel Pavlov
Telerik team
answered on 07 Dec 2012, 09:22 AM
Hello,

I am afraid RadGridView does not offer such feature. The default logic finds the first TextBox and assigns it the focus.
The only way to deal with the problem is to create an user control of your own , containing TextBlocks. In the code behind of this control you will need to implement the logic of replacing the textbox with a textblock thus entering edit mode.
At the final of course you will need to place this user control in the cell edit template.

Greetings,
Pavel Pavlov
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Alex
Top achievements
Rank 1
answered on 24 Jan 2013, 12:09 AM
Pavel,

Could you clarify what you mean about the userControl? I have a user control with an ItemsControl with a text box set as the ItemTemplate.

In the code behind of my view containing the gridView I handle previewKeyDown, find the index of the textbox in which the user is trying to start typing, manually call BeginEdit on the grid and give focus to the correct textbox. However, it seems like something else is making the focus jump back to the first textbox in the cell, rather than the one I was manually trying to focus. How can I prevent the grid from giving the first textbox focus?

Thanks,
Alex
0
Pavel Pavlov
Telerik team
answered on 28 Jan 2013, 01:54 PM
Hello,

The code in RadGridView that forces the focus to the first textbox resides in the PrepareCellForEdit virtual method of the column.

I guess a clean way to prevent RadGridView from forcing the focus would be to inherit GridViewDataColumn and override the PrepareCellForEdit method modifying it in such way that it does not tamper with the focus.

Here is the base method definition.
protected internal override object PrepareCellForEdit(FrameworkElement editingElement, RoutedEventArgs editingEventArgs)
       {
           object originalValue = null;
           if (editingElement == null)
           {
               return null;
           }
           if (this.BindingTarget != null)
           {
               originalValue = editingElement.GetValue(this.BindingTarget);
           }
           if (GridViewBoundColumnBase.HandleIGridViewEditorCase(editingElement, editingEventArgs)
               || GridViewBoundColumnBase.HandleTextBoxCase(editingElement, editingEventArgs))
           {
               return originalValue;
           }
           GridViewBoundColumnBase.SetFocusToElement(editingElement);
           return originalValue;
       }



Regards,
Pavel Pavlov
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

Tags
GridView
Asked by
David
Top achievements
Rank 1
Answers by
Pavel Pavlov
Telerik team
Alex
Top achievements
Rank 1
Share this question
or