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

Support multiple ways of renaming tree node with MVVM

11 Answers 472 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Duo
Top achievements
Rank 1
Duo asked on 29 Sep 2012, 02:34 PM
I am using RadTreeView, there is a request to rename the name of the tree node with the following ways:
1. When the user presses "F2" key
2. When the user double clicks the tree node
3. When the user right click the mouse, select "Rename" from the context menu.

The tree node enters "editing mode" for editing.

The challenging work is to how to implement the above features with MVVM.

Can anyone provide some sample code to demo how to implement that?

Thanks in advance!


11 Answers, 1 is accepted

Sort by
0
Duo
Top achievements
Rank 1
answered on 03 Oct 2012, 01:19 PM
Looks like it is a tough question since no one answers it. I figured that out. Here is the solution:

/*** Begin of the code snippet in the view ***/
<telerik:RadTreeView Grid.Row="0" 
     IsDragDropEnabled="True" 
     IsDragTooltipEnabled="False" 
     x:Name="radTreeView" 
     HorizontalAlignment="Stretch" 
     VerticalAlignment="Stretch" 
     IsLineEnabled="True" 
     Margin="6" 
     IsEditable="True" 
     ItemEditTemplate="{StaticResource TreeNodeEditTemplate}" 
     ItemsSource="{Binding Root}" 
     ItemTemplate="{StaticResource TreeNode}" 
     SelectedItem="{Binding SelectedItem, Mode=TwoWay}"  >
<telerik:RadTreeView.ItemContainerStyle> 
     <Style TargetType="{x:Type telerik:RadTreeViewItem}"> 
         <Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}"/> 
         <Setter Property="IsEditable" Value="{Binding IsEditable, Mode=TwoWay}"/> 
         <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}"/> 
         <Setter Property="IsInEditMode" Value="{Binding IsInEditMode, Mode=TwoWay}"/> 
     </Style>
</telerik:RadTreeView.ItemContainerStyle>
<telerik:RadContextMenu.ContextMenu> 
     <telerik:RadContextMenu x:Name="radContextMenu" ItemsSource="{Binding ContextMenu}"> 
     <i:Interaction.Triggers> 
         <i:EventTrigger EventName="Opened"> 
             <i:InvokeCommandAction Command="{Binding ContextMenuOpenedCommand}"/> 
         </i:EventTrigger> 
     </i:Interaction.Triggers> 
     </telerik:RadContextMenu>
</telerik:RadContextMenu.ContextMenu>
</telerik:RadTreeView>
/*** End of the code snippet in the view ***/

/*** Begin of the code snippet in view model ***/
public bool IsInEditMode

     get { return m_IsInEditMode; } 
     set 
     { 
         if (value != m_IsInEditMode) 
         { 
             m_IsInEditMode = value; 
             OnPropertyChanged(()=>IsInEditMode); 
         } 
     }
}

public bool IsEditable

     get { return m_IsEditable; } 
     set 
     { 
             m_IsEditable = value; 
             OnPropertyChanged(() => IsEditable); 
     } 
}

public ICommand RenameCommand

     get 
     { 
         return m_RenameCommand ?? (new DelegateCommand((param) => 
         { 
             if (!VerifySelectedItem()) 
             return; 
 
             m_ViewModel.SelectedItem.IsInEditMode = true; 
         })); 
     } 
}
/*** End of the code snippet in view model ***/




0
Pavel R. Pavlov
Telerik team
answered on 03 Oct 2012, 02:27 PM
Hi Duo,

The first option (editing a RadTreeViewItem by pressing the "F2" key) is offered by the RadTreeView and you can activate it by setting it`s IsEditable property to True.

The second option (editing a RadTreeViewItem by double clicking on it) can be done by binding the RadTreeViewItem Command property and setting the CommandExecutionTrigger to DoubleClick as shown in the attached project. Please note that you have to bind the IsInEditMode property of the RadTreeViewItem in TwoWay binding mode.

The third option (editing a RadTreeViewItem by choosing "Rename" from a context menu) can be done by using the RadContexMenu control in the RadTreeView.ItemTemplate. You can bind the RadMenuItems Command property to the same ViewModel command definition you use to handle the DoubleClick action.

Kind regards,
TeamX2_Pavel
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Duo
Top achievements
Rank 1
answered on 03 Oct 2012, 06:24 PM
Hi!

Thanks a lot for your sample code, it works as what I want!

Duo
0
Kevin
Top achievements
Rank 2
answered on 13 Feb 2013, 10:24 PM
Thanks for coming back to post your solution!

--Kdc
0
Praveen
Top achievements
Rank 1
answered on 19 Jan 2016, 07:03 AM

For the third option, does it require for the RadContextMenu to be in the RadTreeView.ItemTemplate? We are currently just using ItemsSource to populate our TreeView and don't use item templates at all. However, I am trying to achieve this rename through context menu. I've tried your suggestion, but it doesn't seem to work for me (using IsInEditMode). Please do suggest alternatives.

TIA

0
Martin Ivanov
Telerik team
answered on 21 Jan 2016, 10:43 AM
Hello Praveen,

Another approach could be to define the context menu only for the RadTreeView control and subscribe for the Opening event of the menu. Then get the treeview item under the mouse and get is DataContext which you can set on the context menu. This way you can pass the corresponding treeview item data to the context menu.
<telerik:RadTreeView>
    <telerik:RadContextMenu.ContextMenu>
        <telerik:RadContextMenu Opening="RadContextMenu_Opening">      
        </telerik:RadContextMenu>
    </telerik:RadContextMenu.ContextMenu>
</telerik:RadTreeView>

private void RadContextMenu_Opening(object sender, Telerik.Windows.RadRoutedEventArgs e)
{
    var hoveredElement = (FrameworkElement)Mouse.DirectlyOver;           
    var hoveredTreeViewItem = ParentOfTypeExtensions.ParentOfType<RadTreeViewItem>(hoveredElement);
    var contextMenu = (RadContextMenu)sender;
    contextMenu.DataContext = hoveredTreeViewItem.Header;
}

I hope this helps.

Regards,
Martin
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Swapnil
Top achievements
Rank 1
answered on 02 Apr 2018, 05:32 PM

Hi,

I am facing issue in RadTreeview Edittemplate on Mouse double click

RadTreeView:

 <telerik:RadTreeView x:Name="tvwList" IsDragDropEnabled="True" AllowDrop="False"   PreviewDragEnded="tvwShowsList_PreviewDragEnded"   
                             IsDragPreviewEnabled="True" IsExpandOnSingleClickEnabled="True"  ItemContainerStyle="{StaticResource ItemContainerStyle}"
                             ItemsSource="{Binding Source={StaticResource ShowsCollection}, Path=ShowDataSource}" IsDragTooltipEnabled="False"  Margin="8"
                             ItemTemplate="{StaticResource RootTemplate}" ItemEditTemplate="{DynamicResource EditShowTemplate}" IsEditable="True" ItemsIndent="1">
                        <telerik:RadTreeView.Resources>

 

EditTemplate:

      <StackPanel Orientation="Horizontal">
            <CheckBox Margin="0" IsChecked="{Binding Path=IsOwned, Mode=TwoWay}" />
             <TextBox  Margin="10" FontSize="12" FontFamily="Arial"  Text="{Binding Path=Subject, Mode=TwoWay}"/>
        </StackPanel>

 

 

 

 

I have above EditTemplate which should display on MouseDoubleclick event. When clicked on Node it fired MouseDoubleclicked event and show textbox and check box for fraction of second and then fired Edited event. When we checked using debugger, it works

 

When clicked on left corner of node then it called MouseDoubleclick event but this time it retain the state and after hitting enter button it called Edited event. And using F2 key it works correctly.

If we set <StackPanel Orientation="Vertical"> then it works perfectly fine. But that design not acceptable.

 

0
Martin Ivanov
Telerik team
answered on 05 Apr 2018, 06:48 AM
Hello Swapnil,

I am not sure that I fully understand your requirement. I tried to set up a runnable project and test this but without the item templates and the items container style I cannot tell what happens. May I ask you to prepare a small and runnable project that illustrates the issue and send it in a new support ticket? Thank you for the cooperation.

Regards,
Martin Ivanov
Progress Telerik
Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which allow you to write beautiful native mobile apps using a single shared C# codebase.
0
Swapnil
Top achievements
Rank 1
answered on 05 Apr 2018, 08:44 AM

Hi,

Please find attach demo project to replicate the issue.

Try to Mousedouble click on RadTreeViewNode text part it not worked.

But, If you click on Left side of the radTreeNode it works. or If you change the StackPanel Orientation to  "Vertical" then also it start working.

0
Martin Ivanov
Telerik team
answered on 10 Apr 2018, 08:34 AM
Hello Swapnil,

The reported behavior appears because the treeview commits the edited item on mouse left button down. In other words if you are in edit mode and you click with the mouse the changes are commited.  When you double click the treeview enters edit mode but the click also commits the change and that is why the node flickers.

To resolve this instead of MouseDoubleClick you can use the ItemDoubleClick event of RadTreeView.
private void radTreeView_ItemDoubleClick(object sender, Telerik.Windows.RadRoutedEventArgs e)
{
    var item = (RadTreeViewItem)e.OriginalSource;
    item.BeginEdit();
}

Regards,
Martin Ivanov
Progress Telerik
Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which allow you to write beautiful native mobile apps using a single shared C# codebase.
0
Swapnil
Top achievements
Rank 1
answered on 12 Apr 2018, 01:03 PM
Hi Martin,

Thanks for the suggestion, it works for me.

Tags
TreeView
Asked by
Duo
Top achievements
Rank 1
Answers by
Duo
Top achievements
Rank 1
Pavel R. Pavlov
Telerik team
Kevin
Top achievements
Rank 2
Praveen
Top achievements
Rank 1
Martin Ivanov
Telerik team
Swapnil
Top achievements
Rank 1
Share this question
or