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

4 posts, 0 answers
  1. David
    David avatar
    5 posts
    Member since:
    Oct 2012

    Posted 06 Dec 2012 Link to this post

    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.
  2. Pavel Pavlov
    Admin
    Pavel Pavlov avatar
    2039 posts

    Posted 07 Dec 2012 Link to this post

    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.

  3. Alex
    Alex avatar
    23 posts
    Member since:
    Oct 2012

    Posted 23 Jan 2013 Link to this post

    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
  4. Pavel Pavlov
    Admin
    Pavel Pavlov avatar
    2039 posts

    Posted 28 Jan 2013 Link to this post

    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.

Back to Top