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

DoubleClick in MVVM DataBound TreeView

1 Answer 657 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Ashley
Top achievements
Rank 2
Ashley asked on 30 Mar 2015, 12:30 PM
Hi guys

I have toyed all morning with this problem. I have a DataBound RadTreeView (with ItemsSource) that displays a Hierarchical data structure.

Because each item is displayed using the hierarchy I have no way of hooking into the "DoubleClick" event on a per-item basis.

After some time, I ended up binding SelectedItem to my ViewModel and hooking into the ItemDoubleClick event like below. Unfortunately this makes the assumption that SelectedItem is fired before ItemDoubleClick. So if possible I would love to know a way to hook into the DoubleClick event, so that I can bind the CommandParameter to the item that was double clicked.

<telerik:RadTreeView x:Name="ServerExplorerTreeView" Grid.Row="1" Margin="0 -2 0 0"
VerticalAlignment="Stretch" Background="{StaticResource LightGrayBackgroundBrush}"
IsLineEnabled="True" ScrollViewer.HorizontalScrollBarVisibility="Auto" IsVirtualizing="True"
SelectionMode="Multiple" IsEditable="True" IsDragDropEnabled="True" ItemsSource="{Binding NavigatorVM.Root.ItemsView.View}"
SelectedItem="{Binding NavigatorVM.SelectedItem,Mode=TwoWay}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="ItemDoubleClick">
<i:InvokeCommandAction Command="{Binding NavigatorVM.ItemDoubleClickedCommand}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</telerik:RadTreeView>







1 Answer, 1 is accepted

Sort by
0
Martin Ivanov
Telerik team
answered on 01 Apr 2015, 08:17 AM
Hello Ashley,

You can use our EventToCommandBehavior to attach your command to the ItemDoubleClick event and pass the arguments of the event to the command parameters. This way you will be able to get the double clicked item and modify it as desired.
public class MyViewModel
{
    public DelegateCommand ItemDoubleClickedCommand { get; set; }
    public MyViewModel()
    {
        this.ItemDoubleClickedCommand = new DelegateCommand(ExecuteItemDoubleClick);
    }
 
    private void ExecuteItemDoubleClick(object obj)
    {
        var clickedItem = obj as MyDataItem;
        // execute your logic
    }
}

<telerik:RadTreeView Name="tree">
    <telerik:EventToCommandBehavior.EventBindings>
        <telerik:EventBinding EventName="ItemDoubleClick" Command="{Binding ItemDoubleClickedCommand}" CommandParameter="{Binding ElementName=tree, Path=SelectedValue}" />
    </telerik:EventToCommandBehavior.EventBindings>
</telerik:RadTreeView>
However, keep in mind that the teeview's selection will be changed on the first click of the item. On the second click the item will be already selected. If the event to command behavior doesn't work for you, can you tell me what you want to achieve? This will allow me to think of an approach that could be suitable for your scenario and guide your further.

Thank you for any help you can provide.

Regards,
Martin
Telerik
 

See What's Next in App Development. Register for TelerikNEXT.

 
Tags
TreeView
Asked by
Ashley
Top achievements
Rank 2
Answers by
Martin Ivanov
Telerik team
Share this question
or