Command Binding MVVM

2 posts, 0 answers
  1. richard
    richard avatar
    3 posts
    Member since:
    Oct 2016

    Posted 31 Oct Link to this post

    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

  2. Ivan Ivanov
    Admin
    Ivan Ivanov avatar
    1128 posts

    Posted 03 Nov Link to this post

    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!
  3. UI for WPF is Visual Studio 2017 Ready
Back to Top