I'm trying to set up a command on a button in my UI using MVVM. The command doesn't execute when I click the button, though. The code is based off of Jason Dolinger's example (link in 3rd paragraph).
It seems like it should be pretty simple, so I'm sure I'll feel silly once I find out what's wron开发者_如何学JAVAg.
Relevant code bits follow. The command is as follows (very simple):
public class NavigateCommand : ICommand
{
public bool CanExecute(object parameter)
{
return true;
}
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
public void Execute(object parameter)
{
MessageBox.Show("Executed.");
}
}
The XAML looks like:
<Button x:Name="btn_ProjectManager" Command="{Binding Navigate}" Content="Test Button">
The ViewModel looks like:
public class HomeScreenViewModel : DependencyObject
{
public ICommand Navigate;
public HomeScreenViewModel()
{
this.Navigate = new NavigateCommand();
}
}
Navigate
should be a property. Binding works only with properties
精彩评论