2016-06-12 3 views
1

뷰 및 modelview 개체가 인스턴스화되어주의가 산만 해졌습니다. 예 : 보기 Vlistview LV 및 버튼이 있습니다. 버튼에는 매개 변수로 사용되는 바운드 명령 인 listview LV이 있습니다. 명령 CanExecute 메서드는 listView LV에 요소가 있는지 여부를 확인합니다. 하지만 내가 열 때 보기 V보기 모델 개체는 보기 전에 생성됩니다 않습니다. 따라서 CanExecture 메서드가 listView을 검사하면 null이고 내 버튼은 영원히 사용할 수 없게됩니다.WPF View 및 ModelView 만들기

어떻게 해결할 수 있습니까?

편집 : 명령 실행 : 명령 구현의 종류

public class DelegateCommand : ICommand 
{ 
    public event EventHandler CanExecuteChanged; 

    private readonly Action<object> _execute; 
    private readonly Func<object, bool> _canExecute; 

    public DelegateCommand(Action<object> execute, Func<object, bool> canExecute = null) 
    { 
     if (execute == null) 
     { 
      throw new ArgumentNullException(nameof(execute)); 
     } 

     this._execute = execute; 
     this._canExecute = canExecute; 
    } 

    public bool CanExecute(object parameter) 
    { 
     return _canExecute == null || _canExecute(parameter); 
    } 

    public void Execute(object parameter) 
    { 
     _execute(parameter); 
    } 

    public void RaiseCanExecuteChanged() 
    { 
     CanExecuteChanged?.Invoke(this, EventArgs.Empty); 
    } 
} 
+0

당신은 명령 implementaion/당신이 명령에 사용하는 프레임 워크를 게시 할 수 있습니까? – JanDotNet

답변

0

, 당신은 (목록보기 항목을 가지고 후 귀하의 경우) 수동으로 RaiseCanExecuteChanged()를 호출해야합니다. 구현이 자동으로 CanExecute을 평가하는 명령 관리자를 enbales 그건

[...] 

public event EventHandler CanExecuteChanged 
{ 
    add { CommandManager.RequerySuggested += value; } 
    remove { CommandManager.RequerySuggested -= value; } 
} 

[...] 

:

은 또한에 CanExecuteChanged 이벤트의 구현을 변경할 수 있습니다.

+0

out of topic : 뷰가 생성 될 때 modelview 생성자에 인수를 전달하는 방법을 알고 있습니까? – igorr

+0

@igorr : 별도의 질문을 만드십시오. 코멘트는 그것을위한 것이 아닙니다. – JanDotNet