Commandbindings through xaml
How can I invoke execute开发者_Python百科 and Canexecute methods through XAML?
Execute and CanExecute are members of the interface ICommand, as such you cannot use it just in xaml. The only commands you can use in xaml only, are the system implemented commands such as copy, cut and paste which can be used on a textbox (for example). You must have backing for your implementation of ICommand, you can bind to a command from a view model, but there must be code.
<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;
}
精彩评论