2010-01-25 61 views

回答

1
<Window.CommandBindings> 
    <CommandBinding Command="Help" CanExecute="HelpCanExecute" Executed="HelpExecuted" /> 
</Window.CommandBindings> 
<Button Command="Help" Content="Help Command Button" /> 


private void HelpCanExecute(object sender, CanExecuteRoutedEventArgs e) 
{ 
    e.CanExecute = true; 
    e.Handled = true; 
} 

private void HelpExecuted(object sender, ExecutedRoutedEventArgs e) 
{ 
    MessageBox.Show("Hey, I'm some help."); 
    e.Handled = true; 
} 
+0

沒有cs請。通過XAML實現 – jawahar 2010-01-25 05:56:38

+1

Iam不太確定它是否只能通過XAML完成。無論如何,還有一個建議是這樣的。 <![CDATA [ 空隙ButtonClickMethod(對象發件人,EventArgs的) { btn1.Background = Brushes.Green!; MessageBox.Show(「Red Button clicked」); } ]]> 2010-01-25 06:17:46

1

執行和CanExecute是接口的ICommand的成員,因此你不能只在XAML中使用它。只能在xaml中使用的唯一命令是系統執行的命令,例如複製,剪切和粘貼,可用於文本框(例如)。您必須爲ICommand的實現提供支持,您可以綁定到來自視圖模型的命令,但必須有代碼。