我正在使用MVVM开发我的第一个WPF应用程序,与大学有关。尽管我已经按照之前成功的步骤进行了操作,但我无法使特定的绑定工作。
我有以下XAML代码片段:
<Button Command="{Binding GetTicketsCommand}" Canvas.Left="50" Canvas.Top="202" Content="Refresh List" Height="25" Width="137" />
视图将VM初始化如下:
public JobListView()
{
InitializeComponent();
viewModel = new JobListViewModel(this);
this.DataContext = viewModel;
}
视图模型具有名为GetTicketsCommand的命令,按钮绑定到该命令,但当我单击按钮时,命令Execute或CanExecute方法不会被调用。我创建的命令在VM构造函数中被实例化。
有任何想法吗?
编辑:
命令类如下:
public class GetTicketsCommand : ICommand
{
private readonly JobListViewModel viewModel;
public GetTicketsCommand(JobListViewModel viewModel)
{
this.viewModel = viewModel;
}
public bool CanExecute(object parameter)
{
return true;
}
public void Execute(object parameter)
{
viewModel.GetTickets();
}
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
}
在ViewModel中,我只需要在构造函数中创建一个它的实例:
public JobListViewModel()
{
GetTicketsCommand = new GetTicketsCommand(this);
dataAccess = new DataLayerClient();
}