Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3300822
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T20:41:45+00:00 2026-05-17T20:41:45+00:00

I have a Control inside a Canvas and I want to be able to

  • 0

I have a Control inside a Canvas and I want to be able to move it using the arrow keys. For the sake of trying things out, I created the following class, which does what I want.

<Window x:Class="DiagramDesigner.CanvasControlArrowKeyTest"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        Title="CanvasControlArrowKeyTest" Height="300" Width="300">
    <Canvas>
        <Canvas.InputBindings>
            <KeyBinding Key="Down" Command="MoveDown" />
            <KeyBinding Key="Up" Command="MoveUp" />
            <KeyBinding Key="Right" Command="MoveRight" />
            <KeyBinding Key="Left" Command="MoveLeft" />
        </Canvas.InputBindings>
        <Button>
            <Button.CommandBindings>
                <CommandBinding Command="MoveDown" Executed="MoveDown_Executed" />
                <CommandBinding Command="MoveUp" Executed="MoveUp_Executed" />
                <CommandBinding Command="MoveLeft" Executed="MoveLeft_Executed" />
                <CommandBinding Command="MoveRight" Executed="MoveRight_Executed" />
            </Button.CommandBindings>
        </Button>
    </Canvas>
</Window>

Here’s a snippet of the code-behind:

private void MoveDown_Executed(object sender, ExecutedRoutedEventArgs e)
{
    var uiElement = (UIElement)sender;
    double value = Canvas.GetTop(uiElement);
    value = Double.IsNaN(value) ? 0 : value;
    value++;
    Canvas.SetTop(uiElement, value < 0 ? 0 : value);
}

This all works fine, but what I really want is a bunch of Buttons with this ability, not just that one. How can I make sure every button has these CommandBindings? If there’s an easier way than using CommandBindings, what might that be?

Update: By request, here is another method that doesn’t seem to work:

<Window x:Class="DiagramDesigner.CanvasControlArrowKeyTest"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        Title="CanvasControlArrowKeyTest" Height="300" Width="300">
    <Window.CommandBindings>
        <CommandBinding Command="MoveDown" Executed="MoveDown_Executed" />
        <CommandBinding Command="MoveUp" Executed="MoveUp_Executed" />
        <CommandBinding Command="MoveLeft" Executed="MoveLeft_Executed" />
        <CommandBinding Command="MoveRight" Executed="MoveRight_Executed" />
    </Window.CommandBindings>
    <Window.InputBindings>
        <KeyBinding Key="Down" Command="MoveDown" />
        <KeyBinding Key="Up" Command="MoveUp" />
        <KeyBinding Key="Right" Command="MoveRight" />
        <KeyBinding Key="Left" Command="MoveLeft" />
    </Window.InputBindings>
    <Canvas >
        <Button Width="50" Height="50" />
    </Canvas>
</Window>

C#

private void MoveDown_Executed(object sender, ExecutedRoutedEventArgs e)
{
    var uiElement = (UIElement)e.OriginalSource; // Still doesn't point to the Button
    double value = Canvas.GetTop(uiElement);
    value = Double.IsNaN(value) ? 0 : value;
    value++;
    Canvas.SetTop(uiElement, value < 0 ? 0 : value);
}

Update: I gave up on this approach. I ended up using a different solution for the problem, one that doesn’t use commands.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T20:41:45+00:00Added an answer on May 17, 2026 at 8:41 pm

    You should take a look at routed event handlers: http://msdn.microsoft.com/en-us/library/ms742806.aspx

    The link above has an example of how to do what you’re asking.

    <Border Height="50" Width="300" BorderBrush="Gray" BorderThickness="1">
      <StackPanel Background="LightGray" Orientation="Horizontal" Button.Click="CommonClickHandler">
        <Button Name="YesButton" Width="Auto" >Yes</Button>
        <Button Name="NoButton" Width="Auto" >No</Button>
        <Button Name="CancelButton" Width="Auto" >Cancel</Button>
      </StackPanel>
    </Border>
    

    And the code

    private void CommonClickHandler(object sender, RoutedEventArgs e)
    {
      FrameworkElement feSource = e.Source as FrameworkElement;
      switch (feSource.Name)
      {
        case "YesButton":
          // do something here ...
          break;
        case "NoButton":
          // do something ...
          break;
        case "CancelButton":
          // do something ...
          break;
      }
      e.Handled=true;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have one control named thumbviewer inside repeater. I want to set its imageurl
I created a custom canvas control inheriting from WPF Canvas . I am using
I have a PlaceHolder control inside of a ListView that I am using to
I have a simple page with canvas control. Inside canvas I have some SL
I have a windows form control inside wpf application using a windows forms host.
I have a menu control inside of an updatepanel. When I hover over a
I have an ESRI dropdown control inside of an ESRI toolbar. One of the
I have an ASP.Net CheckBoxList control inside an Ajax UpdatePanel. I will include the
I'm building an asp.net cutom control inside which I have two dropdownlists: companyIdSelection and
I have MyContainer User Control with Grid inside. Each cell of this Grid contains

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.