Button is not enabled even after providing CommandParameter in WPF

I created a button whose command parameter is set and a command using a class that implements the ICommand interface. But my button is disabled. Why is this? I got this code from here: ICommand looks like a chocolate cake

<Window x:Class="ICommand_Implementation_CSharp.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:ICommand_Implementation_CSharp" Title="MainWindow" Height="350" Width="525"> <Grid> <Grid> <Grid.Resources> <local:HelloWorldCommand x:Key="hwc" /> </Grid.Resources> <Button Command="{StaticResource hwc}" CommandParameter="Hello" Height="23" HorizontalAlignment="Left" Margin="212,138,0,0" Name="Button1" VerticalAlignment="Top" Width="75">Button</Button> </Grid> </Grid> 

and my class

 class HelloWorldCommand:ICommand { public bool CanExecute(object parameter) { return parameter != null; } public event EventHandler CanExecuteChanged; public void Execute(object parameter) { MessageBox.Show(parameter.ToString()); } } 
+4
source share
3 answers

Well, this is a very very simple implementation of ICommand .

As @JleruOHeP says, in part, the problem can be resolved using Command and CommandParameter wildcard devices. But this is ugly because you have to remember the sequence every time.

A more correct way is to instruct the CommandManager re-query the command states:

 public class HelloWorldCommand : ICommand { public bool CanExecute(object parameter) { return parameter != null; } public event EventHandler CanExecuteChanged { add { CommandManager.RequerySuggested += value; } remove { CommandManager.RequerySuggested -= value; } } public void Execute(object parameter) { MessageBox.Show(parameter.ToString()); } } 

Now the sequence of setters is indifferent.
To understand how CommandManager works, you can read this good article by Josh Smith.

+5
source

The simplest answer is where you switch the Command and Command parameters:

 <Button CommandParameter="Hello" Command="{StaticResource hwc}" .../> 

But better one is given by @Dennis

+5
source

In my case, it was the CommandParameter type that was causing the problem. My button was simply connected as follows:

 <Button Content="New" Command="{Binding NewCommand}" CommandParameter="False" /> 

The foundation of NewCommand is RelayCommand<bool> . Somehow XAML could not translate False into bool. (Note that it works for many built-in types and properties, maybe some TypeConverter or something in action there).

The solution was to simply spell XAML with a false CommandParameter type, like this:

 <Button Content="New" Command="{Binding NewCommand}"> <Button.CommandParameter> <sys:Boolean> False </sys:Boolean> </Button.CommandParameter> </Button> 

You need to import the sys namespace at the top of your XAML file, for example:

 xmlns:sys="clr-namespace:System;assembly=mscorlib" 

Hope this helps someone along the way.

0
source

All Articles