C# 未在WPF应用程序中执行ICommand
我有一个带有按钮的WPF应用程序,我绑定到C# 未在WPF应用程序中执行ICommand,c#,wpf,binding,icommand,C#,Wpf,Binding,Icommand,我有一个带有按钮的WPF应用程序,我绑定到MessageCommand。为了简单起见,该应用程序只有一个按钮,应该显示一个messagebox。但是,当我单击按钮时,什么也没有发生。我遵循的指示,但我不知道我遗漏了什么,尽管有大量关于这个主题的问题和答案 XAML文件: <Window x:Class="CommandTestProject.MainWindow" ... xmlns:local="clr-namespace:CommandTestProje
MessageCommand
。为了简单起见,该应用程序只有一个按钮,应该显示一个messagebox。但是,当我单击按钮时,什么也没有发生。我遵循的指示,但我不知道我遗漏了什么,尽管有大量关于这个主题的问题和答案
XAML文件:
<Window x:Class="CommandTestProject.MainWindow"
...
xmlns:local="clr-namespace:CommandTestProject"
xmlns:vw="clr-namespace:CommandTestProject.ViewModel"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.DataContext>
<vw:ViewModelMsg/>
</Window.DataContext>
<Grid>
<Button Content="Click"
Command="{Binding Path=MessageCommand}"/>
</Grid>
</Window>
ShowMessage
类:
using CommandTestProject.ViewModel;
using System;
using System.Windows.Input;
namespace CommandTestProject.Commands
{
public class ShowMessage : ICommand
{
private ViewModelMsg viewModel;
public event EventHandler CanExecuteChanged;
public ShowMessage(ViewModelMsg vm)
{
viewModel = vm;
}
public bool CanExecute(object parameter)
{
return true;
}
public void Execute(object parameter)
{
viewModel.Message();
}
}
}
在视图模型中,将
internal ICommand MessageCommand
更改为public ICommand MessageCommand
,它就会工作。绑定到内部属性将不起作用,因为绑定是由单独程序集中的绑定引擎(PresentationFramework.dll)解析的。在视图模型中,将内部ICommand MessageCommand
更改为公共ICommand MessageCommand
,它将起作用。绑定到内部属性将不起作用,因为绑定由单独程序集中的绑定引擎(PresentationFramework.dll)解析。Spot on!非常感谢。完全正确非常感谢。
using CommandTestProject.ViewModel;
using System;
using System.Windows.Input;
namespace CommandTestProject.Commands
{
public class ShowMessage : ICommand
{
private ViewModelMsg viewModel;
public event EventHandler CanExecuteChanged;
public ShowMessage(ViewModelMsg vm)
{
viewModel = vm;
}
public bool CanExecute(object parameter)
{
return true;
}
public void Execute(object parameter)
{
viewModel.Message();
}
}
}