때로는 복잡한 방법을 여러 번 사용했기 때문에 작업을 수행하는 가장 간단한 방법을 잊어 버렸습니다.WPF에서 명령을 바인딩하는 방법
나는 명령 바인딩을하는 법을 알고 있지만, 나는 항상 같은 접근법을 사용한다.
ICommand 인터페이스를 구현하는 클래스를 만들고 뷰 모델에서 해당 클래스의 새 인스턴스를 만들고 매력처럼 작동합니다.
이
내가public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = this;
testCommand = new MeCommand(processor);
}
ICommand testCommand;
public ICommand test
{
get { return testCommand; }
}
public void processor()
{
MessageBox.Show("hello world");
}
}
public class MeCommand : ICommand
{
public delegate void ExecuteMethod();
private ExecuteMethod meth;
public MeCommand(ExecuteMethod exec)
{
meth = exec;
}
public bool CanExecute(object parameter)
{
return false;
}
public event EventHandler CanExecuteChanged;
public void Execute(object parameter)
{
meth();
}
}
바인딩 명령에 사용되는 코드입니다하지만 난이 작업을 수행 할 수있는 기본적인 방법을 알고 싶어, 더 타사 새로운 클래스 생성을 dll을하지 않습니다. 단일 클래스를 사용하여이 간단한 명령 바인딩을 수행하십시오. 실제 클래스는 ICommand 인터페이스에서 구현하고 작업을 수행합니다.
확실히 제 3 자로 간주됩니다. 나는 thirsd 파티에 대한 원한의 어떤 종류도 가지고 있지 않다. GalaSoft MVVM을 사용하고 있지만 간단하고 기본적인 기본적인 실제 방법을 알고 싶습니다. 어쨌든 고마워. – MegaMind
실제로 질문을 읽은 유일한 사람으로 Upvoted. – psycho
@psycho 내 고통을 이해하는 유일한 사람인 것에 대한 건설적인 의견. :-) – MegaMind