MVVM에서 사용되는 명령 바인딩에서 완전히 손실되었습니다. 윈도우에 객체를 바인딩하고 컨트롤에 대한 명령을 호출하여 Button
Click
메서드를 호출하려면 어떻게해야합니까?WPF MVVM : ICommand 컨트롤에 바인딩
public class CustomerViewModel : ViewModelBase
{
RelayCommand _saveCommand;
public ICommand SaveCommand
{
get
{
if (_saveCommand == null)
{
_saveCommand = new RelayCommand(param => this.Save(), param => this.CanSave);
NotifyPropertyChanged("SaveCommand");
}
return _saveCommand;
}
}
public void Save()
{
...
}
public bool CanSave { get { return true; } }
...
ViewModelBase
가 여기에 INotifyPropertyChanged
인터페이스 를 구현하는 명령에 바인딩하는 방법을 Button
입니다 :
<Button Content="Save" Margin="3" Command="{Binding DataContext.Save}" />
CustomerViewModel
의 인스턴스가 DataContext
에 할당 된 다음
CustomerViewModel
클래스
Button
을 포함하는 창
주어진 예제가 작동하지 않습니다. 중단 점을 Save
메서드에 넣었지만 실행이 메서드에 전달되지 않습니다. 나는 (stackoverflow 너무) 예제를 많이 봤지만 바인딩을 지정하는 방법을 알아낼 수 없습니다.
어떤 도움을 주시면 감사하겠습니다.
감사합니다.
P. 아마 내가 바인딩 버튼에 RelativeSource
를 지정해야합니다 ... 이런 식으로 뭔가 :
Command="{Binding Path=DataContext.Save, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"
그러나이 유형의 조상에 대해 지정되어야한다?
프로그램 출력에 바인딩 오류가 있습니까? 바인딩 오류는 종종 감지되지 않습니다. Snoop을 시도해야합니다. 실행중인 응용 프로그램에 연결하고 바인딩 오류를 발견 할 수있는 도구입니다. – Thorsten79