Intersoft ClientUI Documentation
How-to: Implement Mouse Input to a Control with No Command Support

This example shows how to implement mouse input to a UIElement or control that does not support ICommandSource.

Example

Description

The routed command model is mainly designed to separate the semantics and the object that invokes a command from the logic that executes the command. This design enables a routed command to be higly reusable and accessible through a number of ways. In addition to the command source implementation in the control level such as in UXButton, a command can also be accessed programmatically through API, or manipulated through input devices such as keyboard, mouse and touch devices.

With the built-in input binding classes such as MouseBinding, it is possible to add command support to a UIElement that does not implement the commanding architecture. The following example shows how to configure an Image to execute the Copy command when you click on the Image while pressing the Ctrl modifier key.

For MouseGesture XAML attribute usages, the property that is generally set in XAML is Gesture, in cases where the gesture can represent both a mouse action and one or more modifier key. You can also set the Gesture property to be just a mouse action. In general, it is recommended that you use only the Gesture attribute from XAML, rather than setting MouseAction, even if you do not specify modifiers. This will avoid ambiguity, provides the most streamlined syntax, and provides the most straightforward representation for serialization.

Code

XAML
Copy Code
<Intersoft:UXPage 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        xmlns:Intersoft="http://intersoft.clientui.com/schemas"
        xmlns:Commands="clr-namespace:ClientUIApplication_Docs.Commands"
        x:Class="ClientUIApplication_Docs.RoutedCommands.MouseBinding" 
        Title="MouseBinding Page"
        d:DesignWidth="640" d:DesignHeight="480">
        
        <Grid x:Name="LayoutRoot">

        <Intersoft:CommandManager.CommandBindings>
            <Intersoft:CommandBindingCollection>
                <Intersoft:CommandBinding Command="Commands:EditingCommands.Copy"
                                          CanExecute="CopyCmdCanExecute"
                                          Executed="CopyExecuted"/>
            </Intersoft:CommandBindingCollection>
        </Intersoft:CommandManager.CommandBindings>

        <Image Name="image1" Source="../Images/BookShelf.png" Width="250" VerticalAlignment="Center">
            <Intersoft:CommandManager.InputBindings>
                <Intersoft:InputBindingCollection>
                    <Intersoft:MouseBinding Command="Commands:EditingCommands.Copy" 
                                               Gesture="Ctrl+LeftClick"/>
                </Intersoft:InputBindingCollection>
            </Intersoft:CommandManager.InputBindings>
        </Image>
        </Grid>
</Intersoft:UXPage>
C#
Copy Code
using System.Windows;
using ClientUIApplication_Docs.Commands;
using Intersoft.Client.Framework.Input;
using Intersoft.Client.UI.Navigation;

namespace ClientUIApplication_Docs.RoutedCommands
{
    public partial class MouseBinding : UXPage
    {
        public MouseBinding()
        {
            EditingCommands.Initialize();
            InitializeComponent();
        }

        private void CopyCmdCanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = true;
        }

        private void CopyExecuted(object sender, ExecutedRoutedEventArgs e)
        {
            string command = ((RoutedCommand)e.Command).Name;
            string target = ((FrameworkElement)e.OriginalSource).Name;

            MessageBox.Show("The " + command + " command has been invoked on target object " + target);
        }

    }
}

To learn how to create the routed commands for EditingCommands used in the above example, see How-to: Create Routed Commands In a Static Class.  

See Also

Other Resources

Concepts