Click here to Skip to main content
15,886,625 members
Articles / Desktop Programming / WPF

Automatic Merging of Menus and Toolbars in WPF

Rate me:
Please Sign up or sign in to vote.
4.40/5 (5 votes)
26 Sep 2010CPOL7 min read 42.3K   1.3K   31  
This article describes how menus and toolbars in WPF can be automatically merged.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Reflection;

namespace MergeMenuSample
{
   /// <summary>
   /// Interaction logic for MainWindow.xaml
   /// </summary>
   public partial class MainWindow : Window
   {
      public MainWindow()
      {
         InitializeComponent();
      }

      private void Window_Loaded(object sender, RoutedEventArgs e)
      {
         // Normaly we need to instanciate all merge items here
         // But because of the MergedDictionaries trick we can skip this
         // FindResource("utilityMenu");
      }

      private void UtilityMenuItem_Click(object sender, RoutedEventArgs e)
      {
         // Execute utility code
         MessageBox.Show((string)(sender as MenuItem).Header);
      }

      private void UtilityButton_Click(object sender, RoutedEventArgs e)
      {
         // Execute utility code
         MessageBox.Show((string)(sender as Button).Content);
      }

      private void ExitMenuItem_Click(object sender, RoutedEventArgs e)
      {
         Close();
      }

      /// <summary>
      /// Gets or sets if the Utility items should be visible
      /// </summary>
      public Visibility UtilitiesVisibility
      {
         get { return (Visibility)GetValue(UtilitiesVisibilityProperty); }
         set { SetValue(UtilitiesVisibilityProperty, value); }
      }

      // Using a DependencyProperty as the backing store for UtilitiesVisibility.  This enables animation, styling, binding, etc...
      public static readonly DependencyProperty UtilitiesVisibilityProperty = 
         DependencyProperty.Register("UtilitiesVisibility", typeof(Visibility), typeof(MainWindow), new UIPropertyMetadata(Visibility.Collapsed));


      /// <summary>
      /// Gets or sets if the Undo/Redo commands should be visible
      /// </summary>
      public Visibility CanUndoRedo
      {
         get { return (Visibility)GetValue(CanUndoRedoProperty); }
         set { SetValue(CanUndoRedoProperty, value); }
      }

      // Using a DependencyProperty as the backing store for CanUndoRedo.  This enables animation, styling, binding, etc...
      public static readonly DependencyProperty CanUndoRedoProperty = 
         DependencyProperty.Register("CanUndoRedo", typeof(Visibility), typeof(MainWindow), new UIPropertyMetadata(Visibility.Visible));


      private void UtilitiesActiveCheckBox_Click(object sender, RoutedEventArgs e)
      {
         // swicth the visibility of the utility items
         UtilitiesVisibility = utilitiesActiveCheckBox.IsChecked == true ? Visibility.Visible : Visibility.Collapsed;
      }

      private void CanUndoRedoCheckBox_Click(object sender, RoutedEventArgs e)
      {
         // switch visibility of the Undo/Reo items
         CanUndoRedo = canUndoRedoCheckBox.IsChecked == true ? Visibility.Visible : Visibility.Collapsed;
      }

      private void LoadPluginButton_Click(object sender, RoutedEventArgs e)
      {
         // load a plugin dll
         loadPluginButton.IsEnabled = false;

         // load the assembly
         var assembly = Assembly.Load("MergeMenuPlugin");

         // get the resource dictionary
         var dict = new ResourceDictionary();
         dict.BeginInit();
         dict.Source = new Uri("/MergeMenuPlugin;component/Dictionary.xaml", UriKind.RelativeOrAbsolute);
         dict.EndInit();

         // instanciate the plugin menu (this triggers the merging)
         var m = dict["pluginMenu"];

         // place the user control from the plugin in the main grid
         var c = assembly.CreateInstance("MergeMenuPlugin.UserControl1") as UserControl;
         c.HorizontalAlignment = System.Windows.HorizontalAlignment.Right;
         c.VerticalAlignment = System.Windows.VerticalAlignment.Bottom;
         grid.Children.Add(c);

      }

   }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
Software Developer (Senior) remes GmbH
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions