
January 28, 2010 19:55 by
zhuwei
There are the steps I used to create command and attach command in WPF application. I used WPF Composite as my framework to create WPF application. In the main Shell application's navigation Panel, I attach the command to each button.
1. Definition:
In the static class : appcommands
public static RoutedUICommand ProductSearchRequestCommand { get; private set; }
2. create instance
private static void InitializeCommands()
{
ProductSearchRequestCommand = new RoutedUICommand();
}
3. Event handler enabling or disabling command execution
in the main Module class
private void ProductSearchRequestCommand_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = true;
e.Handled = true;
}
4.Command Event handler in the main module class. Here we will execute business logic
private void ProductSearchRequestCommand_Executed(object sender, ExecutedRoutedEventArgs e)
{
regionManager.RegisterViewWithRegion(RegionNames.SearchRegion, typeof(ProductSearchCriteriaView));
regionManager.RegisterViewWithRegion(RegionNames.MainRegion, typeof(ProductSearchResultsView));
}
5.Creat in application's command binding, we attach it to the application's command binding.
CommandBinding binding = new CommandBinding(
AppCommands.ProductSearchRequestCommand,
ProductSearchRequestCommand_Executed,
ProductSearchRequestCommand_CanExecute);
Application.Current.MainWindow.CommandBindings.Add(binding);
6. Attach the command to screen. In the XAML file.
<Button Content="Search Product"
Command="local:AppCommands.ProductSearchRequestCommand"
VerticalAlignment="Top" Height="23.463">
</Button>
Once you set up the first command. It should be easy to creat the left.
47f72126-baa9-4579-85f8-f9d18a08a8de|0|.0