Click here to Skip to main content
15,895,142 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi,
I have been banging my head against the wall on this one.

Until now I have used the code behind for button logic (I know bad.). so I decided to creat a command for my buttons. My command code:

<Button Name="AddUser" Command="{Binding Path=AddUserCommand}" CommandParameter="AddUser" Content="New User" Margin="20" />

And My command code:

public ICommand AddUserCommand { get; set; }


public bool CanExecute(object parameter)
{
if (parameter != null)
{
return true;
}
else
{
return false;
}
}

public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}

public void Execute(string parameter)
{
if (parameter == "AddUser")
{
var window = new AddUser();
window.Show();
}
}
I have set break points and my command code never gets touched.

I don't see where My problem is.

Thanks in advance!
Posted
Comments
tgrt 27-Oct-13 19:46pm    
Is that all your code? Because, I don't see where your command is being created. You have a property, but you're not setting it. You're binding to that property which is a null reference... So I'm not sure what you're expecting.
David C# Hobbyist. 27-Oct-13 19:57pm    
Hmmmm maybe I haven't learned enough yet. Should I be setting this in my ViewModel constructor?
tgrt 27-Oct-13 19:59pm    
See solution below.

1 solution

In your ctor do the following (you might have to implement the DelegateCommand - it's present in Prism):
C#
AddUserCommand = new DelegateCommand(Execute, CanExecute);

Also change the following:
* Change parameter type in Execute to object and cast it within the function
* Get rid of CanExecuteChanged
* Consider not using the parameter at all in Execute and XAML, because you don't need it
 
Share this answer
 
Comments
tgrt 27-Oct-13 20:00pm    
The following site has a good implementation of the DelegateCommand if you're not using Prism. http://blog.rdeverett.com/post/812093504/wpf-delegatecommand
David C# Hobbyist. 27-Oct-13 20:03pm    
There is more to the if statement then I included. I was trying to reuse the Execute method for more than 1 command.
Prism available from nuget i presume.
I will try Your suggestion.
tgrt 27-Oct-13 20:11pm    
You can name the Execute method anything you want. That is, your command instantiation can be: AddUserCommand = new DelegateCommand(AddUser, CanAddUser);

In that case, you'd have two methods with the same signature as Execute and CanExecute currently have.

The only reason to use a parameter in your scenario is if you want poor maintainability. In other words, there's no good reason to do it that way.

You can also pass in lambdas to the instantiation if you have encapsulated an object that handles this type of stuff. For example:

AddUserCommand = new DelegateCommand(parm => userManager.AddUser(), parm => userManager.CanAddUser);

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900