2013-04-02 2 views
0
내 사용자 정의 명령이 실행되지 않습니다

:바운드 명령이 실행되지 않습니다

XAML :

<Button Command="{Binding NewServer}" Content="Save" /> 

뒤에 XAML의 코드 :

public partial class MainWindow : Window { 
    public MainWindow() { 
     InitializeComponent(); 

     DataContext = new ServerViewModel(); 
    } 
} 

ServerViewModel :

public class ServerViewModel : DependencyObject { 
    public ICommand NewServer; 

    private readonly Dispatcher _currentDispatcher; 

    public ServerViewModel() { 
     NewServer = new NewServerCommand(this); 
     _currentDispatcher = Dispatcher.CurrentDispatcher; 
    } 

    public void SaveNewServers() { 
     throw new NotImplementedException("jhbvj"); 
    } 
} 

새 서버 명령 :

public class NewServerCommand : ICommand { 
    public event EventHandler CanExecuteChanged { 
     add { CommandManager.RequerySuggested += value; } 
     remove { CommandManager.RequerySuggested -= value; } 
    } 
    private readonly ServerViewModel _vm; 

    public NewServerCommand(ServerViewModel vm) { 
     _vm = vm; 
    } 

    public bool CanExecute(object parameter) { 
     Dispatcher _currentDispatcher = Dispatcher.CurrentDispatcher; 
     Action dispatchAction =() => MessageBox.Show("asd"); 
     _currentDispatcher.BeginInvoke(dispatchAction); 

     return true; 
    } 

    public void Execute(object parameter) { 
     _vm.SaveNewServers(); 
    } 
} 

CanExecute 또는 Execute가 호출되지 않습니다. 나는 무엇을 잘못 했는가?

답변

4
public ICommand NewServer; 

은 필드입니다. WPF는 필드 바인딩을 지원하지 않습니다. 속성 만. 변경 :

public ICommand NewServer {get;set;} 
+0

오른쪽 ... 감사합니다. –

관련 문제