Quantcast
Channel: Visual Studio Integrate forum
Viewing all articles
Browse latest Browse all 4410

Dynamic adding WPF Controls to Visual Studio Toolbox

$
0
0

What I want to do is to add some WPF controls of an assembly to the Visual Studio 2013 Toolbox. I don't want to do this manualy. Instead I want to creat a vsix project. The resulting installer should add the controls.

What I have done succesfully adding Controls which are in the same assembly by adding an RegistrationAttribute. But in my case I want to add controls of other assemblies dynamicly.

It would be perfect if I can do something like this: Create a vs Package and add the following class:

 [PackageRegistration(UseManagedResourcesOnly = true)]
  [InstalledProductRegistration("#110", "#112", "1.0", IconResourceID = 400)]
    [Guid(GuidList.guidControls_VSPackagePkgString)]
    [ProvideToolboxItems(1)]
    public sealed class Controls_VSPackagePackage : Package//, IVsInstalledProduct
    {
         #region Private Constants

        /// <summary>
        ///   File name of the assembly to be integrated.
        /// </summary>
        private const string ComponentFile = "WpfControlLibrary1.dll";

        /// <summary>
        ///   Toolbox tab name to be created.
        /// </summary>
        private const string TabName = "Test Tab";

        #endregion

        #region Public Constructors

        public Controls_VSPackagePackage()
        {
            ToolboxInitialized += OnToolboxInitialized;
            ToolboxUpgraded += OnToolboxUpgraded;
        }

        #endregion

        #region Private Methods

        /// <summary>
        ///   Called when Visual Studio Toolbox is initialized.
        /// </summary>
        private void OnToolboxInitialized(object sender, EventArgs eventArgs)
        {
            RemoveToolboxItems();
            InstallToolboxItems();
        }

        /// <summary>
        ///   Called when Visual Studio Toolbox is updated.
        /// </summary>
        private void OnToolboxUpgraded(object sender, EventArgs eventArgs)
        {
            InstallToolboxItems();
        }

        /// <summary>
        ///   Install toolbox items from assembly to Toolbox.
        /// </summary>
        private void InstallToolboxItems()
        {
            IToolboxService toolboxService = (IToolboxService)GetService(typeof(IToolboxService));
            var types = GetAssembly().GetTypes();
	    // types[0] is our userControl
            var item = new ToolboxItem(types[0]);
            item.DisplayName = "Test";
            toolboxService.AddToolboxItem(item);

        }

        /// <summary>
        ///   Remove toolbox items contained in assembly from Toolbox.
        /// </summary>
        private void RemoveToolboxItems()
        {
            IToolboxService toolboxService = (IToolboxService)GetService(typeof(IToolboxService));

            var types = GetAssembly().GetTypes();        
            var item = new ToolboxItem(types[0]);
            toolboxService.RemoveToolboxItem(item);
        
        }     

        /// <summary>
        ///   Get AssemblyName of the assembly to integrate.
        /// </summary>
        private Assembly GetAssembly()
        {
            string pathAssembly = String.Concat(
                Path.GetDirectoryName(GetType().Assembly.Location),
                Path.DirectorySeparatorChar,
                ComponentFile);

            return Assembly.LoadFrom(pathAssembly);
        }

        #endregion      
    }

This works fine for Windows Forms but not for WPF controls. 

Is there a way to do something similar with wpf controls?



Viewing all articles
Browse latest Browse all 4410

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>