Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio 2010 XAML Editor awkward IntelliSense?

In Visual Studio C# text editor, when you want a property, you just type prop and then hit tab TWICE to get a "snippet"/template for a property. Both the Type and the property name are highlighted. You press tab when you want to switch between type and property, then you press Enter when you're done and your cursor will jump at the end of the property.

In the WPF XAML editor, pressing "Enter" will create a line break. Like if you type the following:

<TextBox x:

and when you select the "Name" property from the IntelliSense, it will give you the following:

<TextBox x:Name="_"

_ represents your current cursor location.

Give the Name a value, say textbox, then press Enter. At this point, I expect my cursor to jump at the end like so:

<TextBox x:Name="textbox"_ 

but instead it creates a line-break like so:

 <TextBox x:Name="textbox
          _"

This behavior is annoying me. Instead of pressing Enter, I'm pressing the End key instead.

How do I get my intended behavior?

PS: I'm new to WPF and XAML.

like image 882
Ian Avatar asked Feb 12 '11 07:02

Ian


2 Answers

Another simpler solution is to rebind the shortcut key for the operation Edit.WordNext.

By default this is set to Ctrl+RightArrow. In visual studio go to TOOLS->Options->Environment->Keyboard.

I like to bind this to Shift+Space so I don't have to use the arrow keys. When you've finished typing the xaml attribute and the cursor is on the left hand side of the end quotation mark, hit Shift+Space and it jumps to the right hand side of the mark so you can carry on typing the rest of the attributes.

A lot easier than using a macro.

like image 96
megamania Avatar answered Oct 22 '22 05:10

megamania


That's always annoyed me too. One option is to create a simple macro.

  1. Type your XAML tag and name it. (At this point your cursor is inside the quotation marks of the attribute.)
  2. Press Ctrl-Shift-R to start macro recording.
  3. Press End, and then Enter. (Now your cursor is where you want it to be.)
  4. Press Ctrl-Shift-R again to stop macro recording.

If you follow the steps above, Visual Studio will have generated the following macro code, which you can see if you press Alt-F8 and open RecordingModule.TemporaryMacro:

DTE.ActiveDocument.Selection.EndOfLine()
DTE.ActiveDocument.Selection.NewLine()

You can now run this macro with Ctrl-Shift-P, but it's only a temporary macro at this point. You should save it and assign it to a keyboard shortcut.

  1. Open Macro Explorer (Alt-F8).
  2. Find TemporaryMacro under MyMacros | RecordingModule, and rename it. Maybe even move it out of RecordingModule to a different module too.
  3. Open the Tools menu and go to Options, Environment, Keyboard.
  4. Type the name of your macro under "Show commands containing".
  5. Select your macro and assign it shortcut key(s). (I chose Ctrl-Enter.)

So now, instead of moving your hand to press End and then Enter, you can just press Ctrl-Enter.

I also recorded another macro that moves to the end of the line and types " />" for me, and attached that to Ctrl-/. The code VS generates looks like this:

DTE.ActiveDocument.Selection.EndOfLine()
DTE.ActiveDocument.Selection.Text = " />"
DTE.ActiveDocument.Selection.NewLine()

An alternative to all these macros is to turn off the automatic quotes. Open the Tools menu and go to Options, Text Editor, XAML, Miscellaneous. Uncheck the option for auto-inserting attribute quotes. Then it won't add the ending quote for you, and you won't have to use the arrow keys or the End key (although you will have to type the ending quote now, of course).

like image 37
Andrew Avatar answered Oct 22 '22 05:10

Andrew