How to build dynamic menus and toolbars

The challenge is as following:

  • I want to have external XAML files with menus and toolbars
  • I do not want to recompile 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

What’s the problem? The answer is XamlReader. First let’s create menus and toolbars in external loose XAML files and put them into our BIN directory.

Menu1.xaml

<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>

ToolBar1.xaml

<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>

Now let’s create out window

Window1.xaml

<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, the only thing we should do it to read Menu1.xaml and TooBar1.xaml from our BIN directory and add Menu and ToolBar controls dynamically to the page

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);
               }
           }
       }

image

We done. Have a nice day :)

Source code for this article

8 Responses to “How to build dynamic menus and toolbars”

  1. Suresh Says:

    I have created menu using the below code.But I could n’t attach the onclick event of a menu item.

    <Page.Resources>

    <XmlDataProvider x:Key="Menus" XPath="menus/menu" Source="menu.xml">

    </XmlDataProvider>

    <HierarchicalDataTemplate x:Key="MenuTemplate" ItemsSource="{Binding XPath=menu}"  >

    <AccessText Text="{Binding XPath=@name}"/>

    </HierarchicalDataTemplate>

    <Style x:Key="Triggers" TargetType="{x:Type MenuItem}">

    <Style.Triggers>

    <Trigger Property="MenuItem.IsMouseOver" Value="true">

    <Setter Property = "Foreground" Value="Red"/>

    <Setter Property = "FontSize" Value="16"/>

    <Setter Property = "FontStyle" Value="Italic"/>

    </Trigger>

    </Style.Triggers>

    </Style>

    </Page.Resources>

    <StackPanel>

    <!– Binding in XAML –>

    <Menu Name="mnuHeader" Style="{StaticResource Triggers}"  ItemsSource="{Binding Source={StaticResource Menus}}" ItemTemplate="{StaticResource MenuTemplate}" />

    </StackPanel>

  2. Tamir Khason Says:

    Renuka, actually, if you have handlers in your code. Just add events to your menu items

  3. Renuka Says:

    How to attach events with dynamic generated menus

  4. Adriana Says:

    How do I give functionality to this controls. They don't seem to work once I run the application. Can you give an example on this?

    I would really appreciate it.

    Regards..

  5. עומר.נט Says:

    There are quite a few sources about how to create dynamic menus (menus that are not embedded in XAML

  6. Omer van Kloeten's .NET Zen Says:

    There are quite a few sources about how to create dynamic menus (menus that are not embedded in XAML

  7. Noticias externas Says:

    The challenge is as following: I want to have external XAML files with menus and toolbars I do not want

  8. Sook Vanakin Says:

    I am glad to be a visitor of this unadulterated site ! , regards for this rare information! .

Leave a Reply

Recommended

 


Sponsor


Partners

WPF Disciples
Dreamhost
Code Project
Switched to Better Place

Together