MVVM binding a double click to a method using telerik radtreecontrol

I am working on this issue for a dumb amount of time. It is time to ask for directions, even though my inner man said, "Don't do this."

I am coding in WPF C # using the MVVM design pattern. We try to strictly adhere to the template and do not put anything in the code if there is no choice, or it is completely unreasonable. Having said that, I work with Telerik RadTreeView. Here is a snippet of it in my XAML:

<telerik:RadTreeView IsExpandOnSingleClickEnabled="True" IsLineEnabled="True" Margin="5" ItemsSource="{Binding ItemsView}" SelectedItem="{Binding SelectedWidget, Mode=TwoWay}" ItemTemplate="{StaticResource NodeTemplate}" /> 

Currently, the tree is working correctly, so if you select a tree element and click OK on the view, everything will be fine. However, I also need to allow the user to double-click on one of the tree items. This means that I already have a command and a protected method that overrides void OkAction () in my view model with the necessary logic. Telerik provides the ItemDoubleClick property, which should provide the double-click functionality of the tree structure. But I can’t find anything that would allow me to do this in the view model. In other words, how do I bind? We also have a behavior setup in our project for double-clicking, which I was told that I can use, but I have no experience in behavior. I'm still a little wet with WPF.

If this helps, here is the link to the documentation for Telerik: http://www.telerik.com/help/wpf/radtreeview-events-overview.html

I would be grateful for any help or guidance that anyone can provide.

Try this: Stan:

 <Grid.Resources> <DataTemplate x:Key="WidgetTemplate"> <StackPanel Orientation="Horizontal"> <Image Source="/Resources/gear1.png" Margin="1" Stretch="None" /> <TextBlock Text="{Binding Name}" VerticalAlignment="Center" Margin="6,0,0,0" /> </StackPanel> </DataTemplate> <HierarchicalDataTemplate x:Key="NodeTemplate" ItemsSource = "{Binding Children}" ItemTemplate="{StaticResource WidgetTemplate}"> <TextBlock Text="{Binding Name}"/> </HierarchicalDataTemplate> </Grid.Resources> 
+6
source share
3 answers

Here you will want to use the Attached Behavior that you already have for DoubleClick.

Otherwise, here is the complete code that I use that creates the Attached Behavior and will create two Attached Properties that are bound to the command and optionally a command parameter.

AttachedBehaviors.cs

 public static class MouseDoubleClick { public static DependencyProperty CommandProperty = DependencyProperty.RegisterAttached("Command", typeof(ICommand), typeof(MouseDoubleClick), new UIPropertyMetadata(CommandChanged)); public static DependencyProperty CommandParameterProperty = DependencyProperty.RegisterAttached("CommandParameter", typeof(object), typeof(MouseDoubleClick), new UIPropertyMetadata(null)); public static void SetCommand(DependencyObject target, ICommand value) { target.SetValue(CommandProperty, value); } public static void SetCommandParameter(DependencyObject target, object value) { target.SetValue(CommandParameterProperty, value); } public static object GetCommandParameter(DependencyObject target) { return target.GetValue(CommandParameterProperty); } private static void CommandChanged(DependencyObject target, DependencyPropertyChangedEventArgs e) { Control control = target as Control; if (control != null) { if ((e.NewValue != null) && (e.OldValue == null)) { control.MouseDoubleClick += OnMouseDoubleClick; } else if ((e.NewValue == null) && (e.OldValue != null)) { control.MouseDoubleClick -= OnMouseDoubleClick; } } } private static void OnMouseDoubleClick(object sender, RoutedEventArgs e) { Control control = sender as Control; ICommand command = (ICommand)control.GetValue(CommandProperty); object commandParameter = control.GetValue(CommandParameterProperty); if (command.CanExecute(commandParameter)) command.Execute(commandParameter); } } 

.xaml . Remember to add the namespace where the Attached Behavior is located.

 <telerik:RadTreeView IsExpandOnSingleClickEnabled="True" IsLineEnabled="True" Margin="5" ItemsSource="{Binding ItemsView}" SelectedItem="{Binding SelectedWidget, Mode=TwoWay}" ItemTemplate="{StaticResource NodeTemplate}" acb:MouseDoubleClick.Command="{Binding ShowItemCommand}" /> 

SampleViewModel.cs

 private RelayCommand _showItemCommand; public RelayCommand ShowItemCommand { get { return _showItemCommand ?? (_showItemCommand = new RelayCommand(ShowItemDetails, IsItemSelected)); } } 
+4
source

obviously I don't have a Telerik code, so I can't help as much as I would like, but you can try something like this. (Disclaimer: I am writing from above)

Define Your Style in Grid.Resources

 <Style TargetType="{x:Type RadTreeViewItem }" x:Key="TreeViewItemStyle"> <EventSetter Event="MouseDoubleClick" Handler="{Binding DoubleClick}" /> </Style> 

Add style to container style.

 <telerik:RadTreeView IsExpandOnSingleClickEnabled="True" IsLineEnabled="True" Margin="5" ItemsSource="{Binding ItemsView}" SelectedItem="{Binding SelectedWidget, Mode=TwoWay}" ItemTemplate="{StaticResource NodeTemplate}" ItemContainerStyle ="{StaticResource TreeViewItemStyle}"/> 

Let me know if it works.

0
source

I tried several ways to achieve this. In the end, I found that VS2012 is giving me the opportunity. I noticed that the changes were not applied during assembly and launch.

I opened VS2010 to find that I did not have the same problems. After talking with my boss, we found this to be a good example of a situation where sticking to MVVM might not be the wisest choice, since double-clicking was strictly related to the UI.

I simply bounced through the code behind and into the view model, using an instance of the view model as the data context. It did not take a second.

As for the other solutions, I am sure that it is quite possible, but I can not confirm or deny the messages that I made here due to my VS2012 problems.

0
source

All Articles