Minggu, 08 September 2013

How to Add Menu Control: WPF

Any GUI window in our computer like Notepad, MS Word, browsers or even visual studio have its own menu bar. As compare to other controls Menu bar helps user to do the desired task in simple & easy way. Menu bar can be added as the Menu control in programming languages.

WPF menu control enables the user to organize elements in hierarchy view. To add a menu bar, you have to just drag-n-drop the Menu control from the toolbox or write following code in XAML:

<Menu/>

It may be our main menu with IsMainMenu property to true. Writing above line of code will show nothing in the window, till we don’t add any item in it. Item can be added using MenuItem class with Header property.

In the following lines of XAML code, I have add three items in the menu bar.
<Menu IsMainMenu="True">
<MenuItem Header="First"></MenuItem>
<MenuItem Header="Second"></MenuItem>
<MenuItem Header="Third"></MenuItem>
</Menu>

Just run the code and it will show a window containing a menu bar with three items:

Menu bar control in WPF

Now as in other windows, user presses the left Alt key and menu bar has been highlighted, keyboard shortcut cab also be used by using underscore before the character as in following code.
<Menu IsMainMenu="True">
<MenuItem Header="_First"></MenuItem>
<MenuItem Header="_Second"></MenuItem>
<MenuItem Header="_Third"></MenuItem>
</Menu>

Now it will be used by keyboard shortcut as in other windows. Run the project and press left alt key it will highlight the menu bar as in following image:

Highlighted menu bar control WPF

Tidak ada komentar:

Posting Komentar