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.
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.
That's always annoyed me too. One option is to create a simple macro.
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.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With