dotnetspider.com
Login Login    Register      

TutorialsForumCareer DevelopmentResourcesReviewsJobsInterviewCommunitiesProjectsTraining

Subscribe to Subscribers
Talk to Webmaster
Tony John

Facebook
Google+
Twitter
LinkedIn
Online Membersashwini gawade
Amruta
Alwyn Duraisingh
Shine S
Chandra
Devaraj T N
More...
Join our online Google+ community for Bloggers, Content Writers and Webmasters




Resources » .NET programming » WPF

Menu Control in WPF


Posted Date:     Category: WPF    
Author: Member Level: Gold    Points: 10



 


In this article i will be exploring the different aspects of Menu Control in WPF. I assume that the audience are familier with WPF and Declarative programming using XAML. In brief WPF is the next generation of User Interface designing. XAML helps in making clear separation between Design and code behing logic for design. Explaining about these is out of scope of this article, i go ahead with the assumption that you know basics of XAML and WPF.

Menus has been an integral part of any Windows based application. In WPF there are two classes that are used for making Menu; Menu and MenuItem. A Menu is a collection of one or more MenuItem. Each MenuItem can have a Command associated with it. A MenuItem again may further have MenuItem associated with it to make submenus. In this article i will show how to work with menus.

Lets start by looking how to create a Menu (XAML Code).

< Menu Name="Menu1" HorizontalAlignment="Left" VerticalAlignment="Top"/ >

This tag creates a Menu Control. Now lets look at some major properties of Menu Control.
Name --> Defines the name of the menu.
Height --> Defines the Height of the menu
Width --> Defines the width of the menu
Margin --> Defines the position of the menu
Background--> Defines backcolor of the menu
HorizontalAlignment --> Defines the horizontal alignment of the menu inside the parent control.
HorizontalContentAlignment --> Defines the horizontal alignment for the menu content.
VerticalAlignment --> Defines the vertical alignment of the menu inside the parent control.
VerticalContentAlignment --> Defines the content alignment for the menu content.
ToolTip --> defines the tooltip for the menu.

Now that we have seen the major properties its time to see them in action. A Menu tag with all its properties set may look like:

< Menu Height="20" Width="200" Name="Menu1" HorizontalAlignment="Left" HorizontalContentAlignment="Left" VerticalAlignment="Top" VerticalContentAlignment="Top" Background="BlanchedAlmond">


In this sample we have set properties of the menu in XAML, but this is not the only way of doing so. Properties of a menu can be set in three different ways:
In XAML manually as done above
Using Property Window
using codebeind in C# at runtime.

Now that our menu control is ready lets add some Menu items to it.

As said earlier MenuItem tag is used to add menu item to a menu.

< MenuItem Header="_File"/>

Here we create a Menu Item with name as File.

A menu item can in turn have other menu items within it as sub menus. Now lest add some more menu items to our File menu.

< MenuItem Header="_File">
< MenuItem Header="_New"/>
< MenuItem Header="_Open"/>
< Separator/>
< MenuItem Header="_Save"/>
< MenuItem Header="Save _All"/>
< /MenuItem>

Here we have added 4 menu items and a separator to the File Menu

A separator is used to categorize similer menu items. It is added with < Separator/> tag.

We can also add sub menu items to sub menus.

The xaml will look like

< MenuItem Header="_File">
< MenuItem Header="_New"/>
< MenuItem Header="_Open"/>
< Separator/>
< MenuItem Header="_Save">
< MenuItem Header="Save _File"/>
< MenuItem Header="Save A_ll"/>
< MenuItem Header="Save _As"/>
< /MenuItem>
< /MenuItem>


Now lets add a tool tip to our menu item.


< MenuItem Header="_New">
< MenuItem.ToolTip>
< ToolTip>
Create new File
< /ToolTip>
< /MenuItem.ToolTip>
< /MenuItem>

Now lets look at adding and event handler to menu item.


< MenuItem Header="_New" Click="MenuItem_Click"/>


The Click event is used to associate an event handler to the menu item.

And in the code behind


private void MenuItem_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("Menu item Clicked");
}

Here i show a message box to user when menu item is clicked.

With this i end my article on Menu control in WPF. In my next article i will touch upon more advanced parts of menu like Creating menu at runtime and command binding.





Did you like this resource? Share it with your friends and show your love!


Responses to "Menu Control in WPF"

No responses found. Be the first to respond...

Feedbacks      

Post Comment:




  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)



    Type the numbers and letters shown on the left.


    Next Resource: Creating Pop-up window in XAML Browser Application
    Previous Resource: WPF - Overview and Sample Code Snippets in C#
    Return to Resources
    Post New Resource
    Category: WPF


    Post resources and earn money!
     
    More Resources
    Popular Tags   Tag posting guidelines   Search Tags  
    WPF  .  MenuItem  .  



    Follow us on Twitter: https://twitter.com/dotnetspider

    Active Members
    TodayLast 7 Daysmore...

    Awards & Gifts
    Email subscription
  • .NET Jobs
  • .NET Articles
  • .NET Forums
  • Articles Rss Feeds
    Forum Rss Feeds


    About Us    Contact Us    Copyright    Privacy Policy    Terms Of Use    Revenue Sharing sites   Advertise   Talk to Tony John
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India
    2005 - 2012 All Rights Reserved.
    .NET and other trademarks mentioned in this site belong to Microsoft and other respective trademark owners.
    Articles, tutorials and all other content offered here is for educational purpose only.
    We are not associated with Microsoft or its partners.