5,695,118 members and growing! (16,594 online)
Email Password   helpLost your password?
Platforms, Frameworks & Libraries » Windows Presentation Foundation » General     Beginner License: The Code Project Open License (CPOL)

How to Build Dynamic Menus and Toolbars in WPF?

By Tamir Khason

This article explains how to use build in features of WPF to insert menus, toolbars or any other content dynamically into your application
C# 3.0, C#Windows, .NET, .NET 3.0, .NET 3.5, WinXP, Vista, XAML, WPF, VS2005, VS2008, Visual Studio, Dev

Posted: 1 Nov 2007
Updated: 1 Nov 2007
Views: 12,868
Bookmarked: 29 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
5 votes for this Article.
Popularity: 2.22 Rating: 3.18 out of 5
2 votes, 40.0%
1
0 votes, 0.0%
2
0 votes, 0.0%
3
0 votes, 0.0%
4
3 votes, 60.0%
5

Introduction

WPF is a very dynamic language. It contains very smart objects, enables you to load, parse and display content dynamically. More than this, you can even create custom event handlers (commands) and bind them into loose XAML files that will be loaded in runtime, whenever you need it without recompiling application code.

Background

The challenge is as follows:

  • I want to have external XAML files with menus and toolbars
  • I do not want to recompile a project if I want to change something in those menus and toolbars
  • I want to provide commands for those menus and toolbars without recompiling
  • I want to write least amount of code to provide such functionality

Using the Code

Let's create our menu in the Menu.xaml file. This is a loose XAML file and it does not contain C# code behind.

<Menu xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 
  <MenuItem Header="Edit"> 
    <MenuItem Command="ApplicationCommands.Cut"/> 
    <MenuItem Command="ApplicationCommands.Copy"/> 
    <MenuItem Command="ApplicationCommands.Paste"/> 
  </MenuItem> 
</Menu>

I put into this file everything I'll use in the application. In this case, those are three common clipboard commands: Cut, Copy and Paste. ApplicationCommand will take care of those commands and provide me with requested functionality.

Now, let's create a toolbar in the ToolBar1.xaml file.

<ToolBar xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 
  <Button Command="ApplicationCommands.Cut"> 
    <Image Source="pack://siteoforigin:,,,/cut.png"/> 
  </Button> 
    <Button Command="ApplicationCommands.Copy"> 
      <Image Source="pack://siteoforigin:,,,/copy.png"/> 
    </Button> 
    <Button Command="ApplicationCommands.Paste"> 
      <Image Source="pack://siteoforigin:,,,/paste.png"/> 
    </Button> 
</ToolBar>

And last but not least, XAML code for my main Window, named Window1.xaml. Here, I'll put custom style to assure that once any of the Images or its parent controls become disabled, the images become semi-transparent.

<Window x:Class="DynamicMenu.Window1" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="DynamicMenu" Height="300" Width="300" 
    > 
  <Window.Resources> 
    <Style TargetType="Image"> 
      <Style.Triggers> 
        <Trigger Property="IsEnabled" Value="False"> 
          <Setter Property="Opacity" Value="0.5"/> 
        </Trigger> 
      </Style.Triggers> 
    </Style> 
  </Window.Resources> 
    <StackPanel Name="main"> 
      <TextBox/> 
      <TextBox/> 
    </StackPanel> 
</Window>

Now, when we have all XAMLs, we have to load my dynamic menu and toolbar and add this into the application. To perform this action, we'll use XamlReader that will read and parse my loose XAML and add it into the application.

public Window1() 
       { 
           InitializeComponent(); 
           using (FileStream s = new FileStream("Menu1.xaml", FileMode.Open)) 
           { 
               Menu menu = XamlReader.Load(s, new ParserContext()) as Menu; 
               if (menu != null) 
               { 
                   main.Children.Insert(0, menu); 
               } 
           } 
           using (FileStream s = new FileStream("ToolBar1.xaml", FileMode.Open)) 
           { 
               ToolBar tool = XamlReader.Load(s, new ParserContext()) as ToolBar; 
               if (tool != null) 
               { 
                   main.Children.Insert(1, tool); 
               } 
           } 
       } 

Points of Interest

Isn't it nice that you have not recompiled anything in order to achieve very rich functionality?

History

  • 1st November, 2007: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Tamir Khason


Hello! My name is Tamir Khason, and I am software architect, project manager, system analyst and [of couse] programmer. In addition to writing big amount of documentation, I also write code, a lot of code. I work as a freelance architect, project manager, trainer, and consultant here, in Israel. I have very pretty wife and 3 charming kids, but unfortunately almost no time for them. If you are in need of my services, please check out my experience, skillsets and rates. I can be reached at tamir[AT]khason.biz or at www.dotNet.us if you have any questions.
To be updated within articles, I publishing, visit my blog or subscribe RSS feed.





Occupation: Software Developer (Senior)
Company: Sharpsoft
Location: Israel Israel

Other popular Windows Presentation Foundation articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 4 of 4 (Total in Forum: 4) (Refresh)FirstPrevNext
QuestionWPF Project Typemembercyberjp9:07 7 Nov '07  
AnswerRe: WPF Project TypememberTamir Khason20:58 7 Nov '07  
GeneralThis would work for built-in commands but...memberkemetokara16:03 1 Nov '07  
GeneralRe: This would work for built-in commands but...memberTamir Khason21:29 1 Nov '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 1 Nov 2007
Editor: Deeksha Shenoy
Copyright 2007 by Tamir Khason
Everything else Copyright © CodeProject, 1999-2008
Web19 | Advertise on the Code Project