Command's CanExecute method is called but not Execute method - c#

I've bound a command to a button on a Ribbon control. The CanExecute method on the button gets called as expected but clicking on the button doesn't cause the Execute method to be called. The CanExecute sets the CanExecute property to true - the button in question is enabled and clickable.
Has anyone else seen this behaviour before? If so how do I fix it!
EDIT:
CommandBinding commandBinding = new CommandBinding(StaticCommands.ThisCommand, ThisCommandExecutedHandler, ThisCommandCanExecuteHandler);
CommandManager.RegisterClassCommandBinding(this.GetType(), commandBinding);
CommandBindingList.Add(commandBinding);
StaticCommands.ThisCommand is a RoutedCommand with an input gesture of F5.
Unfortunately I can't post any xaml because everything is wrapped up in another team's libraries. I am assuming that is correct for now. Also, using the keyboard gesture associated with the command (pressing F5) causes the execute method to be called.
There are no exceptions thrown, no messages in the output window, and snoop shows everything bound correctly. I'm really stumped.

This usually happens if the parameters dont match in type correctly... are you binding CommandParameter of one type and accepting a different type parameter in the Command.Execute() call?

Fixed this by wrapping the RoutedCommands in a RelayCommand. I have no idea why that worked, but assuming there's a problem in the other team's assembly.

Related

Difference between Command (ICommand) and Click event

When should I use the Command and when to use the Click event?
F.e. if I have a Button in my UWP app what should I use?
When should I use the Command and when to use the Click event?
Yours is a broad question and I would simply answer with: "It depends".
Because:
The Command implements the ICommand interface and this means more code to add to your application but usually this won't change. Instead, the event handler doesn't require any interface implementation.
For every command you want, you have to provide the code that will handle the click and the CanExecute logic, to say when the command can execute. This is not requested in a simple event handler (like MyButton_Click). This means that, using a Command, you will have more control over the elements of your UI (the button won't execute anything if CanExecute is false).
When you want to add a Command, you will bind it to your DataContext (the ViewModel, if you implement the MVVM pattern). Instead, when you add a simple event handler (like MyButton_Click), the code will be placed in your code-behind that is the logic behind your main window. This means that implementing a Command, according to me, you'll have everything you need to modify in just one place (the ViewModel) instead of logic scattered everywhere in your project.
Of course, you can use whatever you want and my points are there just to give you an insight about these different implementations and you have to consider which solution is suitable for you, considering also the requirements you have been given (like: "Don't use event handlers" or "The Command is too advanced, let's just use something simple", etc.) and/or other constraints in your project.

C# WPF add KeyUp event for window with a handler in another page

I'm working on a WPF project and I'm new to it. I organize my project like this:
MainWindow.xaml (Contains a NavigationWindow)
MainWindow.xaml.cs
HomePage.xaml (Contains a series of function)
Homepage.xaml.cs
The thing is that I want to add a KeyUp event to my program, and I want it to call a function in HomePage.xaml.cs when a key is pressed. But I found that it is impossible to add a KeyUp event to a Page object, thus I decided to add it for NavigationWindow. However, I cannot reach the function in HomePage.xaml.cs inside NavigationWindow, so I came up with an idea.
var window = Window.GetWindow(this);
((MainWindow) window).KeyUp += new system.Windows.Input.KeyEventHandler(this.Window_KeyUp);
However, this doesn't work and throw a System.NullReferenceException. I don't know why it happens. Maybe it wants to say that this is null in here? But why?
Though it is silly, I would like to answer to this question myself. (BTW, I haven't received many answers which upsets me. Luckily I found the problem)
The problem is about Window.GetWindow(this). It returns null, which clearly doesn't have a KeyUp event. The solution is to change it to Application.Current.Windows, which is an array of windows hierarchy. Thus Application.Current.Windows[0] can return the MainWindow object, which can be attached with the handler.

How to pass a specific action to a viewmodel to execute on a button press?

I am trying to setup this scenario.
The basic premise is this. I have a message window, with couple of text fields and button, controlled by a viewmodel and a model. Originally window is displayed with showdialog() with button invisible while some background checks are going on.
If a an error occurs i would like the text in the window to change accordingly and a button become visible. I would also like this button to execute a specific action, a delegate or a static somewhere.
I would like to be able to pass this action to the viewmodel to be executed. The reason is i want to be reuse this window for different screen and button actions may change.
Thank you.
1.Define a command in your ViewModel(as a property with INotifyPropertyChanged).
Bind your button to this command:
Command={Binding MyCommand}
where MyCommand=property of yours viewmodel.
In case you need to change command action, just set property MyCommand to the implementation of command you are currently in need of.

CommandBindings / InputBindings and RoutedCommand.InputGestures

I having trouble to understand some of the features in the RoutedCommand.
The RoutedCommand can be created and some input gesture can be attached:
HomeZoomPanCommand = new RoutedCommand();
HomeZoomPanCommand.InputGestures.Add(new KeyGesture(Key.F10, ModifierKeys.None));
Now I have to add the command to the CommandBindings collection which is quite clear to me.
Normally I would assume, that if I want to have F10 working on this command, I have to add a KeyBinding also to the InputBindings. This seems also clear to me.
But what happens in detail when I have the InputGestures already assigned to the RoutedCommand? I assumed that the gesture is automatically added to the InputBindings collection but it is obviously not.
For what reason are there two different ways. Does the InputGesture on the command really work or is it only to show it in the menu with any function?

Issue when using Routed Commands with a Frame

My WPF project has a main window with a number of buttons and a frame. I display a page within the frame, and the buttons above use routed commands in order to call methods present in the page below. With me so far?
Everything works fine until I change the page. Even though this new page has methods which relate to the commands, it seems the routed commands are still looking for methods in the previous page. I don't know why this is happening when I've set the typeof() argument in the command itself:
public static RoutedCommand cmd = new RoutedCommand("Foo", typeof(BarPage));
How can I fix this? I've noticed that setting focus on a textbox in the newly-selected page will correct things, however this isn't a viable solution as the page will not always contain fields. I've also tried setting focus on the page itself but the problem still applies.
Thanks in advance.

Categories

Resources