ISCL is a Intelligent Information Consulting System. Based on our knowledgebase, using AI tools such as CHATGPT, Customers could customize the information according to their needs, So as to achieve

Coding a Menu in WPF

2
Menus are central to most user interfaces these days, so there has to be a way of coding them in WPF and XAML. But since Menus are also a composite kind of thing, there are always a few unique methods involved in creating them. In this example, we use a WPF "User Control" to create the menu so it's a good example of that as well.

The Basic XAML Menu

As you might expect, you can create the menu interface entirely in a single XAML file.

The technique isn't too remarkable. Just use the Menu and MenuItem controls like this:

<DockPanel>
   <Menu>
      <MenuItem Header="_File"
             Click="File_Click"
             ToolTip="Open and Save Files"
             IsCheckable="True"
             />
      <MenuItem Header="_Edit"
             Click="Edit_Click">
         <MenuItem Header="_Cut" />
         <MenuItem Header="C_opy" />
         <MenuItem Header="_Paste" />
      </MenuItem>
      <Separator />
      <MenuItem Header="_About"
             Click="About_Click" />
   </Menu>
</DockPanel>


You can see that I've thrown in a few of the optional properties for the "File" MenuItem just to make sure you're aware that they're available. The underscore character appears before the "access keys" that can be used along with the Alt key to select the MenuItem. This is exactly the way it's always been. I've also included a second level menu with "Cut", "Copy", and "Paste" under the "Edit" menu to show how that works.

And finally, you can use the "Collection Editor" to work with your MenuItem Items as well.

The illustration below shows the Collection Editor in action. Note that all of them are called "MenuItem". That's not very helpful in seeing which one you're working on. The value coded in the Header property is about the most useful identification available. There is no "global" collection of MenuItems in VB.NET 2008. The collection shown is the three underneath the "Edit" MenuItem.

--------
Click Here to display the illustration
Click the Back button on your browser to return
--------

The event code for each MenuItem is in a subroutine like this (this one is for the File menu):

Private Sub File_Click( _
   ByVal sender As System.Object, _
   ByVal e As System.Windows.RoutedEventArgs)


In VB.NET 2008, you can double-click on the control in the designer, just like Windows Forms, or you can right-click on the name in the XAML window (File_Click in the code above) and select the "Navigate to Event Handler" item.

You may be asking, "How does VB.NET supply the code for all of these operations?" The answer is that it doesn't. You do. I used a familiar menu here just to make sure you understand how it all fits together. The code that actually performs a "Cut" or a "Copy" (or whatever you're putting in your menu) still has to be programmed in your event subroutine. Again, that's the way it's always been.

On the next page, I demonstrate how to code the same menu as a "User Control" that will get it out of your main code and let you include it in as many WPF forms as you like.
Source...
Subscribe to our newsletter
Sign up here to get the latest news, updates and special offers delivered directly to your inbox.
You can unsubscribe at any time

Leave A Reply

Your email address will not be published.