See RoutedCommand s.
Define your command in myclass somewhere as follows:
public static readonly RoutedCommand Login = new RoutedCommand();
Now define your button with this command:
<Button Command="{x:Static myclass.Login}" />
You can use CommandParameter for more information.
Now, but just as importantly, start listening to your command:
In the class constructor you want to do something nice, you put:
CommandBindings.Add(new CommandBinding(myclass.Login, ExecuteLogin));
or in XAML:
<UserControl.CommandBindings> <CommandBinding Command="{x:Static myclass.Login}" Executed="ExecuteLogin" /> </UserControl.CommandBindings>
And you implement the delegate that needs CommandBinding:
private void ExecuteLogin(object sender, ExecutedRoutedEventArgs e) { //Your code goes here... e has your parameter! }
You can start listening to this command everywhere in your visual tree!
Hope this helps
PS You can also define a CommandBinding with a CanExecute delegate that will even disable your command if CanExecute says so :)
PPS Here is another example: RoutedCommands in WPF
Arcturus
source share