views:

105

answers:

4

When working with Binding converters are pretty common.. I always find myself

  • Right-click the correct Folder
  • Click Add
  • Click New Item
  • (Sometimes) Choose Code to the left
  • Choose Class and "Add" (and sometimes when I'm in a hurry a create an AboutBox instead :-( )
  • Copy an old IValueConverter or IMultiValueConverter
  • Change the namespace and the class name
  • Remove the old code

and then I can finally start to implement my new converter.

After this I also have to add this namespace to the xaml file and add it to resources before I can reference it. I've been doing this a million times and this is probablly the slowest way to do it so my question is..

What is the fastest way to create a Converter in Visual Studio?

A: 

Keep all of your converter classes in a single file (ie: Converters.cs), then when you need to add one you can do the same copy/paste that you're already doing but you won't have to add a new item, re-namespace, add a new namespace, etc.

At least, that's what I do...

Brian Driscoll
Copy/paste is *never* the right solution...
Thomas Levesque
By the way, are you related to [that guy](http://en.wikipedia.org/wiki/Brian_O'Driscoll) ? ;)
Thomas Levesque
I disagree - Copy/paste is very often not the right solution for most people. Those of us who are not careless are usually able to copy/paste without making mistakes.
Brian Driscoll
Oh, and unfortunately no I am not related...
Brian Driscoll
The styleguides where I work are very strict about 1 class/file so this isn't an option. Thanks anyway!
Meleak
+7  A: 

Probably the best resource for you is the Visual Studio templates. You can create your own and so you could right-click/create new item/Converter that would stub in everything that you're doing manually.

If you do create something like that, it would probably be a great little project to share with the community via codeplex or something like that.

Creating Item Templates in Visual Studio

Dave White
I'll second this. I do this for behaviors, converters, and a few other custom classes on my current project. The time savings isn't as significant as not breaking the flow of my work. Helps keep my head in the project.
Raumornie
Thanks for your answer! I'm gonna try Visual Studio templates! Also I second Raumornie's comment "Helps keep my head in the project".
Meleak
+5  A: 

You can certainly save time on this part:

  • Copy an old IValueConverter or IMultiValueConverter
  • Change the namespace and the class name
  • Remove the old code

Do it this way instead:

  • Create a new class (FooConverter for instance)
  • Make it implement IValueConverter by just adding : IValueConverter
  • With the caret still on IValueConverter, press Ctrl + . to open the smart tag menu
  • Select "Implement interface IValueConverter" (should be the first option) from the smart tag menu

Visual Studio will automatically create the necessary method stubs, you just need to write the implementation.

Thomas Levesque
Why the down-vote ? What's wrong with this answer ?
Thomas Levesque
Thanks for your answer! This is actually my second approach if I don't have any other converter available at the moment. I've always found the copy/paste way to be faster because studio doesn't autocomplete IValueConverter unless the namespace is included. But I guess you are right, this way is probably faster :-) Did you actually get a vote down for this answer? I'll upvote you :)
Meleak
A: 

I have added a Value Converter item template to the Visual Studio 2010: From the Project Context Menu, Add...New Item...Online Templates folder...Silverlight folder...Value Converter.

Michael S. Scherotter