Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fastest way to create a Converter in Visual Studio

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?

like image 565
Fredrik Hedblad Avatar asked Oct 26 '10 15:10

Fredrik Hedblad


2 Answers

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.

like image 126
Thomas Levesque Avatar answered Oct 03 '22 00:10

Thomas Levesque


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

like image 31
Dave White Avatar answered Oct 03 '22 01:10

Dave White