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

Problems while editing a node in RadTreeView

1 Answer 156 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
sonal
Top achievements
Rank 1
sonal asked on 14 Dec 2008, 08:01 AM
Hi,
I am using following code to bind data to tree view. Each node in tree view is bound to business object "Node".

    <telerik:RadTreeView HorizontalAlignment="Left"
                             x:Name="uxOrgTree"
                             Width="Auto"
                             Height="Auto"
                             d:LayoutOverrides="Height"  
                             VerticalAlignment="Top"
                             Margin="0,0,0,0"
                             SelectionMode="Single"
                             IsEditable="True"
                             ItemsSource="{Binding RootNodes}" >
            <telerik:RadTreeView.Resources>
                <HierarchicalDataTemplate DataType="{x:Type Entities:Node}" ItemsSource="{Binding Children}" >
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="{Binding Path= Name}"/>
                    </StackPanel>
                </HierarchicalDataTemplate>
            </telerik:RadTreeView.Resources>
      <telerik:RadTreeView.ItemContainerStyle>
                <Style TargetType="{x:Type telerik:RadTreeViewItem}">
                    <EventSetter Event="Edited" Handler="RadTreeViewItem_Edited" />
                 </Style>
            </telerik:RadTreeView.ItemContainerStyle>
        </telerik:RadTreeView>

Now when i press F2 or call SelectedItem.BeginEdit() programmatically,  i see datacontext which is Entities.Node in my case, in the edit area and the TextBlock defined in the HierarchicalDataTemplate is displayed next to edit area. I want to edit text in the textblock.  How can i do it?



Thanks
Sonal

1 Answer, 1 is accepted

Sort by
0
Miroslav
Telerik team
answered on 17 Dec 2008, 08:37 AM
Hello sonal,

First, sorry for the delayed reply.

Currently the TreeView item calls ToString() of the item and displays it in the TextBox during editing. So you can override the ToString method of the node class with something like:

public override string ToString()  
{  
    return this.Name;  

Then, you need to handle the Edited event (as you are doing). The default behaviour of the TreeView is to assign the result of the editing as a new DataContext. Also there is not binding in action during editing, so you need to update the business object as well, like so:

private void TreeViewItemEdit(object sender, RadTreeViewItemEditedEventArgs e)  
{  
    var treeViewItem = e.OriginalSource as RadTreeViewItem;  
    var node = treeViewItem.DataContext as Node;  
 
    node.Name = e.NewText;  
 
    treeViewItem.Header = node;  

Does that work for you?

Kind regards,
Miroslav
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Tags
TreeView
Asked by
sonal
Top achievements
Rank 1
Answers by
Miroslav
Telerik team
Share this question
or