我有一個ViewModel類,我想響應內置的刷新命令,這是從按鈕觸發的,但我不知道如何聲明CommandTarget。獲取非UI對象來響應WPF命令綁定
簡單地說,我的代碼如下
視圖模型構造和CanExecute和執行的事件處理程序 -
public ViewModel()
{
CommandBinding binding = new CommandBinding(NavigationCommands.Refresh, CommandHandler);
binding.CanExecute += new CanExecuteRoutedEventHandler(binding_CanExecute);
binding.Executed += new ExecutedRoutedEventHandler(binding_Executed);
CommandManager.RegisterClassCommandBinding(typeof(ViewModel), binding);
}
void binding_Executed(object sender, ExecutedRoutedEventArgs e)
{
Debug.Print("Refreshing...");
}
void binding_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = true;
}
標記是 -
<Button Command="Refresh">refresh</Button>
現在,我已經試過設置在這個按鈕上的CommandTarget到{Binding Source={StaticResource ViewModel}}
,但我得到一個運行時說Cannot convert the value in attribute 'CommandTarget' to object of type 'System.Windows.IInputElement'
。
我是新來的命令,所以這是完全可能的我在這裏是各種各樣的錯誤。任何幫助,將不勝感激。
謝謝肯特。這有助於 – Stimul8d 2009-03-03 12:07:58