Thursday, October 18, 2007

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

No comments: