简单的 WPF MVVM 命令问题 - 这段代码有什么问题?
我正在尝试使用 MVVM 在 UI 中的按钮上设置命令。但是,当我单击按钮时,该命令不会执行。该代码基于 Jason Dolinger 的示例(第 3 段中的链接)。
看起来应该很简单,所以当我发现问题所在时,我肯定会觉得自己很傻。
相关代码位如下。命令如下(非常简单):
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.");
}
}
XAML 如下所示:
<Button x:Name="btn_ProjectManager" Command="{Binding Navigate}" Content="Test Button">
ViewModel 如下所示:
public class HomeScreenViewModel : DependencyObject
{
public ICommand Navigate;
public HomeScreenViewModel()
{
this.Navigate = new NavigateCommand();
}
}
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 wrong.
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();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Navigate
应该是一个属性。绑定仅适用于属性Navigate
should be a property. Binding works only with properties