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

Command Binding MVVM

1 Answer 445 Views
GridView
This is a migrated thread and some comments may be shown as answers.
richard
Top achievements
Rank 1
richard asked on 31 Oct 2016, 11:26 AM

Hello,

i am trying to implement a command (LeftDoubleClick) from my view to viewmodel using mvvm pattern (without code behind and event).

I saw some samples but they don't work for me!

Please can someone tell me i i am making wrong?

xaml:

<telerik:RadGridView x:Name="radGridView" Margin="8" ItemsSource="{Binding Source={StaticResource DataSource}}"
   IsReadOnly="True"   >
 
        <telerik:RadGridView.InputBindings>
            <MouseBinding Gesture="LeftDoubleClick" helpers:AttachedMouseBinding.Command="{Binding Path=DoubleClickCommand}" />
        </telerik:RadGridView.InputBindings>
 
    </telerik:RadGridView>

 

.cs:

public class UC_SEGridViewModel : InterfaceDescription.ViewModels.ViewModelBase
{
    private ViewModelMaster viewModelMaster;
    public ICommand DoubleClickCommand { get; set; }
 
    public UC_SEGridViewModel()
    {
        DoubleClickCommand = new RelayCommand(GridItemDoubleClick);
    }
 
    private void GridItemDoubleClick(object obj)
    {
        //do something
    }
}

but i don't reach my callback method (GridItemDoubleClick) when i set a breackpoint

the helper:

public class AttachedMouseBinding
    {
        const string PROPERTYNAMECOMMAND = "Command";
 
        public static readonly DependencyProperty CommandProperty = DependencyProperty.RegisterAttached(PROPERTYNAMECOMMAND, typeof(ICommand),
            typeof(AttachedMouseBinding), new FrameworkPropertyMetadata(CommandChanged));
 
        private static void CommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            FrameworkElement fe = d as FrameworkElement;
             
            ICommand command = e.NewValue as ICommand;
 
            InputBinding inputBinding = new InputBinding(command, new MouseGesture(MouseAction.LeftDoubleClick));
            fe.InputBindings.Add(inputBinding);
 
            //if (inputBinding != null)
            //    inputBinding.Command = command;
        }
 
        public static ICommand GetCommand(DependencyObject obj)
        {
            return (ICommand)obj.GetValue(CommandProperty);
        }
 
        public static void SetCommand(DependencyObject obj,ICommand value)
        {
            obj.SetValue(CommandProperty, value);
        }            
    }

 

Thanks for your help,

Richard

1 Answer, 1 is accepted

Sort by
0
Ivan Ivanov
Telerik team
answered on 03 Nov 2016, 11:17 AM
Hello,

The InputBinding mechanism does not respect handled events. This is a common limitation to use it with custom controls. For example even the WPF TextBox, does not support them for much of the keyboard/mouse gestures, as it handles the respective events for navigation purposes. The case with RadGridView is just the same. It executes the logic for RowActivated at this point thus handling the event. If you want to have a no-code-behind solution for this scenario, you can use an attached behavior that is assigned in XAML, which has the event subscription in its body.

Regards,
Ivan Ivanov
Telerik by Progress
Do you need help with upgrading your WPF project? Try the Telerik API Analyzer and share your thoughts!
Tags
GridView
Asked by
richard
Top achievements
Rank 1
Answers by
Ivan Ivanov
Telerik team
Share this question
or