ItemTemplate - How to reload?

3 posts, 0 answers
  1. Rob
    Rob avatar
    238 posts
    Member since:
    Jan 2009

    Posted 05 Nov 2012 Link to this post


    I have a RadTreeView with some items that have the following edit DataTemplate...  (loaded from an ItemEditTemplateSelector if that matters).  I have omitted some portions of the data template for brevity.
    <DataTemplate x:Key="dt_TagTreeEditorMode_EditTemplate">
          <TextBox x:Name="tb" Text="{Binding Name, Mode=TwoWay}" />
          <FocusHelper:FocusHelper TargetElement="{Binding ElementName=tb}" SetFocusOnStartup="True" />

    The "FocusHelper" control has only one purpose...  When loaded, it will attempt to set focus and select all the text in it's TargetElement (in this case, the TextBox).

    My problem is, that it only seems to happen the first time I go into edit mode on a tree element.  As if it remembers the datatemplate the second time and doesn't fire any loaded events.  As soon as I edit the element a second time, it doesn not fire any constructors or "loaded" events on the FocusHelper, and hence, doesn't focus the TextBox automatically...

    Is there some way to dispose the DataTemplate or get it out of memory so that the next time it's loaded I can set the focus properly?  Normally I use this FocusHelper control inside other controls with vsm's and bind the property to a state and it works perfectly...


  2. Pavel R. Pavlov
    Pavel R. Pavlov avatar
    1236 posts

    Posted 07 Nov 2012 Link to this post

    Hi Rob,

    When you go in edit mode for the first time the RadTreeView control cashes the EditTemplate of the edited item. That's why no events are fired when you try to edit the item for the second time.

    However, as a workaround you can implement your custom focusing logic when the EditStarted event is fired. For example the next snippet will focus and will select all the text inside a TextBox (which is part of the EditTemplate of the edited item):

    private void RadTreeView_EditStarted(object sender, RadTreeViewItemEditedEventArgs e)
     editedItem = e.OriginalSource as RadTreeViewItem;
     Dispatcher.BeginInvoke(() =>
       foreach (TextBox tb in editedItem.ChildrenOfType<TextBox>())
        if (tb.Name == "focusElement")

    The Dispatcher is used to make sure that the EditTemplate is applied to the item. Give it a try and let me know if this workaround works for you.

    All the best,
    Pavel R. Pavlov
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

  3. Rob
    Rob avatar
    238 posts
    Member since:
    Jan 2009

    Posted 15 Nov 2012 Link to this post

    Hi Pavel,

    Thanks for the option.  It seems to work just fine.

Back to Top