WPF ViewModel Commands CanExecute issue

后端 未结 5 779
情书的邮戳
情书的邮戳 2020-12-09 11:38

I\'m having some difficulty with Context Menu commands on my View Model.

I\'m implementing the ICommand interface for each command within the View Model, then creati

5条回答
  •  情话喂你
    2020-12-09 12:24

    Thank you for the speedy replies. This approach does work if you are binding the commands to a standard Button in the Window (which has access to the View Model via its DataContext), for example; CanExecute is shown to be called quite frequently when using the CommandManager as you suggest on ICommand implementing classes or by using RelayCommand and DelegateCommand.

    However, binding the same commands via a CommandReference in the ContextMenu do not act in the same way.

    In order for the same behaviour, I must also include the EventHandler from Josh Smith's RelayCommand, within CommandReference, but in doing so I must comment out some code from within the OnCommandChanged Method. I'm not entirely sure why it is there, perhaps it is preventing event memory leaks (at a guess!)?

      public class CommandReference : Freezable, ICommand
        {
            public CommandReference()
            {
                // Blank
            }
    
            public static readonly DependencyProperty CommandProperty = DependencyProperty.Register("Command", typeof(ICommand), typeof(CommandReference), new PropertyMetadata(new PropertyChangedCallback(OnCommandChanged)));
    
            public ICommand Command
            {
                get { return (ICommand)GetValue(CommandProperty); }
                set { SetValue(CommandProperty, value); }
            }
    
            #region ICommand Members
    
            public bool CanExecute(object parameter)
            {
                if (Command != null)
                    return Command.CanExecute(parameter);
                return false;
            }
    
            public void Execute(object parameter)
            {
                Command.Execute(parameter);
            }
    
            public event EventHandler CanExecuteChanged
            {
                add { CommandManager.RequerySuggested += value; }
                remove { CommandManager.RequerySuggested -= value; }
            }
    
            private static void OnCommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
            {
                CommandReference commandReference = d as CommandReference;
                ICommand oldCommand = e.OldValue as ICommand;
                ICommand newCommand = e.NewValue as ICommand;
    
                //if (oldCommand != null)
                //{
                //    oldCommand.CanExecuteChanged -= commandReference.CanExecuteChanged;
                //}
                //if (newCommand != null)
                //{
                //    newCommand.CanExecuteChanged += commandReference.CanExecuteChanged;
                //}
            }
    
            #endregion
    
            #region Freezable
    
            protected override Freezable CreateInstanceCore()
            {
                throw new NotImplementedException();
            }
    
            #endregion
        }
    

提交回复
热议问题