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

ItemTemplate - How to reload?

2 Answers 25 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Rob
Top achievements
Rank 1
Rob asked on 05 Nov 2012, 09:48 PM
Hi,

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">
 
   <Grid>
 
      <TextBox x:Name="tb" Text="{Binding Name, Mode=TwoWay}" />
 
      <FocusHelper:FocusHelper TargetElement="{Binding ElementName=tb}" SetFocusOnStartup="True" />
    
   </Grid>
 
</DataTemplate>

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 Answers, 1 is accepted

Sort by
0
Pavel R. Pavlov
Telerik team
answered on 07 Nov 2012, 04:41 PM
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")
   {
    tb.Focus();
    tb.SelectAll();
    break;
   }
  }
 });
}

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.

0
Rob
Top achievements
Rank 1
answered on 15 Nov 2012, 07:41 PM
Hi Pavel,

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

-Rob
Tags
TreeView
Asked by
Rob
Top achievements
Rank 1
Answers by
Pavel R. Pavlov
Telerik team
Rob
Top achievements
Rank 1
Share this question
or