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);
}
}
}
We done. Have a nice day
October 17th, 2007 · Comments (8)
8 Responses to “How to build dynamic menus and toolbars”
Leave a Reply
Discover other tags
My tools
- .NET Framework Detector
- Duplicate images finder
- Exchange Security Policy for Windows Mobile Devices Fix
- Gas Price Windows Vista SideBar gadget
- Israel Traffic Information Windows Vista SideBar gadget
- Localization fix for SAP ES Explorer for Visual Studio
- LocTester
- RTL and LTR in Windows Live Writer
- Silverlight controls library
- Snipping tool integration plugin for WLW
- USB FM receiver library
- Vista Battery Saver
- WebCam control for WPF
- Windows Live SkyDrive attachment for Windows Live Writer
- Wireless Migrator
- WPF Virtual Keyboard




January 1st, 2009 at 12:24 am
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>
January 1st, 2009 at 12:24 am
Renuka, actually, if you have handlers in your code. Just add events to your menu items
January 1st, 2009 at 12:24 am
How to attach events with dynamic generated menus
January 1st, 2009 at 12:24 am
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..
January 1st, 2009 at 12:24 am
There are quite a few sources about how to create dynamic menus (menus that are not embedded in XAML
January 1st, 2009 at 12:24 am
There are quite a few sources about how to create dynamic menus (menus that are not embedded in XAML
January 1st, 2009 at 12:24 am
The challenge is as following: I want to have external XAML files with menus and toolbars I do not want
December 29th, 2011 at 1:50 pm
I am glad to be a visitor of this unadulterated site ! , regards for this rare information! .