Tuesday 4 June 2013

Configuring Commands and attaching to the Keyboard and Mouse events in Windows Presenation Foundation.



Commands:


In WPF world, Commands are excuted on events triggered by mouse or keyboard.


They are a loosely coupled way to bind the UI to the logic that performs the action. 


Commands give central Architecure for executing certain tasks from multiple actions taken by User on UI across application.

for example Application.Find command can be excuted upon click on menu or button if this command are binded to that menu or button.

Principle of implementing Command in WPF.

1.Command.It can be ApplicationCommands  for example Find ,copy , paste or Custom commands derived from ICommand interface .

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
namespace WPFCommands.Commands
{
   public  class AddCommand
    {
       static RoutedUICommand _addcommands;
          static AddCommand()
        {
            InputGestureCollection _gesturecol = new InputGestureCollection();
            KeyGesture _key = new KeyGesture(Key.A, ModifierKeys.Control );
           
             MouseGesture _mouseclick = new MouseGesture(MouseAction.LeftClick);
             _gesturecol.Add(_key);
            _gesturecol.Add(_mouseclick);
            _addcommands = new RoutedUICommand("Addcommands", "Addcommands", typeof(AddCommand), _gesturecol);
             

        }
        public static RoutedUICommand Addcommands
        {
            get { return _addcommands; }


2.CommandBinding: To associate a command with source control and handler upon invoking of command.
Add namespace  in Xaml where AddCommand has been defined
<Window 
        xmlns:Local="clr-namespace:WPFCommands.Commands">
    <Window.CommandBindings>
       Add CommandBindings
            <CommandBinding Command="Local:AddCommand.Addcommands"  Executed="_bindings_Executed"  >
                </CommandBinding>
    </Window.CommandBindings>

3.Command source: UI elements for example Button,menu item etc or gesture for example Mouse click or  pressing keyboard keys defined in InputGestureCollection.


    <StackPanel Background="Aquamarine"  HorizontalAlignment="Left" Height="158" Margin="125,105,0,0" VerticalAlignment="Top" Width="358" OpacityMask="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}">
            <TextBlock Background="White"   TextWrapping="Wrap" Text="TextBlock"   Height="37" Width="358"/>
            <Button Height="50px" Background="BlanchedAlmond"  Command="Local:AddCommand.Addcommands"   Content="Add"   />
        </StackPanel>
4.Command Handler: a CommandHandler is method and it is excuted when command is invoked.



    public partial class MainWindow : Window
    {
...
        void _bindings_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            MessageBox.Show("Hello");

        }

        
    }


 This is a much simple example of using command but power of command come in use when   UI code/presentation logic is placed into another class called ViewModal, a MVVM pattern. having Modal and UI logic handling UI events and properties.

No comments:

Post a Comment