tags:

views:

42

answers:

2

How can I invoke execute and Canexecute methods through XAML?

+1  A: 
    <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; }

Kishore Kumar
No cs please. Achieve through XAML
jawahar
Iam not quite sure whether it can done only through XAML. Anyway one more suggestion is this .<x:Code> <![CDATA[ void ButtonClickMethod(object sender, EventArgs e) { btn1.Background = Brushes.Green; MessageBox.Show("Red Button clicked"); } ]]> </x:Code>
Kishore Kumar
+1  A: 

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.

Aran Mulholland