MVVM binding double click to method using telerik radtreecontrol

旧城冷巷雨未停 提交于 2019-12-05 08:46:31

This is where you are going to want to possibly use the Attached Behavior that you already have for the DoubleClick.

Otherwise, here is the complete code that I use which creates the Attached Behavior and will create two Attached Properties which bind to a 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 of where the Attached Behavior lies.

<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));
    }
}

obviously I don't have Telerik code so I can't really help as much as i would like to but you can try something like this. (Disclaimer: I am writing from top of my head)

Define your style in Grid.Resources

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

Add the 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.

flyNflip

I tried several ways to get this accomplished.In the end I found that VS2012 was giving me fits. I noticed that changes weren't being applied on a build and run.

I opened VS2010 to find I wasn't experiencing the same issues. After speaking with my boss, we found this to be a good example of a situation that adhering to MVVM may not be the wisest choice since double clicking was strictly UI related.

I simply bounced through the code behind and into the view model using the instantiation of the view model as the data context. Didn't take but a second to do that.

As for the other solutions, I am sure it is completely possible, but I cannot confirm or deny the posts I've made here because of my VS2012 issues.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!