JG Vimalan's Blog

It is all about coding!

Set hotkey for buttons in XAML application at runtime

I got a task where I need to set hotkeys for the buttons generated based on the records displayed in a grid. Each record displayed in the grid will have a button. The first record which have a button whose shortcut key will be “shift 0”.  Similarly, for every records displayed, a button will be  created whose hotkey will be “shift 1, shift 2… upto shift 9 respectively. So, when the user presses shift 1, the corresponding button’s click event will be invoked. To achieve this, follow the steps given below,

a. Add a string array as shown below in your application. Here, I am using a XAML application for performing this example.

string[] appShiftKey = new string[10] { “)”, “!”, “@”, “#”, “$”, “%”, “?”, “&”, “*”, “(” };

These symbols are available in the keyboard and they were ordered to represent the numbers 0,1, 2, 3..  9 in the keyborad.

b. In the XAML button control, use the content property to set the hotkey.

public string myButtonHotKey
        {
            get { return submitButton.Content.ToString(); }
            set { submitButton.Content = value; }
        }

c. In the code, set the hotkey while iterating throught the records. Limit the record display to 10 in the grid to achieve this fuctionality.

myButtonHotKey = string.Format(“_{0}”, appController.appShiftKey[itemIndex])}

d. The outcome will be as follows, when you click the respective hotkeys, the buttons click event will fire and you can perform desired acition in that event.

Advertisements

November 14, 2010 - Posted by | XAML

No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: