views:

548

answers:

2

I'm making a Component that will be used in a VS drag and drop designer. One of the properties on this component needs to be the pack URI of a file within the project.

I'd like to make things a little easy and, from within the property editor the PropertyGrid uses for my type's property, examine the solution, construct the Uris and present them to the user for choosing.

Is this possible? And, if so, can I get some pointers and where-to-starters on how to go about this?

+1  A: 

Is this a component that is for WPF projects only? Then you may be in luck. Here's a write up. http://www.wiredprairie.us/journal/2007/06/pack_syntax_in_wpf.html. Or this MSDN sample may help http://msdn.microsoft.com/en-us/library/aa972152(VS.85).aspx

Sai Ganesh
Nope. That's the easy part. The hard part is interacting with the Visual Studio project system from within a property grid editor. Nice try.
Will
Have you looked the DTE? Are you using VB, C# or C++?For C# here's what you do to get the environment.Using EnvDTE80; // or EnvDTE90 or EnvDTE depending on the visual studio you're targeting.// Get an instance of the currently running Visual Studio IDE. For 8.0 version EnvDTE80 dte = (EnvDTE80.DTE2)System.Runtime.InteropServices.Marshal.GetActiveObject("VisualStudio.DTE.8.0");
Sai Ganesh
Nope. Seems like you know some stuff about it... Put some code up for getting the files in the active project. That would be very helpful!
Will
Sai Ganesh
Thanks for the help.
Will
+1  A: 

I think there are a couple of peices to this.

1) You can create your own type editors for a property, to decide how the property values are presented to the users using the property grid.

For this, you need to create a type editor, inheriting from UITypeEditor, like this.

public class UriListUIEditor : UITypeEditor
{
       //Override a couple of methods
}

Have a look at this codeproject article to see a simple example. http://www.codeproject.com/KB/edit/flagenumeditor.aspx

Now, provide the EditorType attribute of your property, like

   [Editor(typeof(Utils. UriListUIEditor ), 
             typeof(System.Drawing.Design.UITypeEditor))]
 public string Uri 
             { get;set;
             }

2) To iterate solutions in your project, get the current DTE Instance

 var dte = (EnvDTE80.DTE2)System.Runtime.InteropServices.Marshal. GetActiveObject("VisualStudio.DTE.8.0");

And Iterate through all the projects to build the list or URIs or anything. Ideally you do this inside the EditValue method of the above UriListUIEditor.

  foreach (var project in dte.Solution.Projects)
        {

        }

Hope this helps

amazedsaint
It does. Thanks!
Will
Unfortunately, there are a couple issues with this. First, GetActiveObject snags the first VS from the ROT, which means it could be one of any of the instances of VS you have running. You have to run through the ROT and get the one you need. Second, once you do have your DTE, you can enumerate stuff fine until you actually try to get a value from a property, which throws a SerializableException.
Will
I eventually did something similar to this, only I had to use the Running Object Table to figure out which instance of VS to use. Thanks much for your help. I regret making this a wiki now; I can't revert and accept this as an answer.
Will