When working with WPF or Silverlight, how should one use control naming conventions? Do you name the controls in XAML markup? I have seen samples of projects at codeplex with control names such as "selectButton" or "btnSelect". What would you recommend?
3 Answers
Microsoft has guidelines published here on their web site. The bottom line is that Hungarian naming conventions are out.
EDIT
To make this more clear, Microsoft has dropped Hungarian notation from all of their naming conventions, including UI elements. HOWEVER, MS has not documented any recommendations for UI elements. There are lots of links out there that note this and offer their suggestions but the bottom line is that with UI elements, you're on your own. Example link.
In our standard we've dropped Hungarian notation and are using explicit naming, meaning a button called OK would be named ButtonOK, a textblock called Comments would be TextblockComments. The downside is that names can get kind of long, the positive is that EVERYONE knows exactly what the element is.
As long as you establish what works for you and use that standard consistently, you can't go wrong.
-
2Those are guidelines for naming members in libraries, not UI elements. Nov 25, 2010 at 19:54
-
@Robert - good point. I hadn't noticed that their guideline excluded UI elements. I'll edit my answer.– WalterNov 25, 2010 at 21:40
I usually dont name my controls in XAML, as it would be, most of the time, unused considering everything is set or controlled through bindings. Source : Pete Brown
-
The same article says that you're going to have to name all of your data entry elements (textboxes, checkboxes, combos) anyway, since they will be referred to elsewhere (a data store, for example). The chrome elements (lines, shapes and such) don't have to be named, and it's nice that XAML doesn't force you to do so. Nov 25, 2010 at 19:42
-
@Robert Harvey : From the article : "Interactive UI controls like TextBoxes, ListBoxes, Buttons etc. You can get away without naming these if you're using commands/behaviors and a good pattern like MVVM, but I find naming is helpful from a documentation standpoint. Not a requirement by any means, but helpful.". As I went with MVVM, and dont have to communicate my xaml to a designer for blend work, I didnt have find any use for this names. My controls being really simple, the documentation provided by those names would be overkill. But I agree that on more complex UI, it could be different.– MatthieuNov 25, 2010 at 19:56
-
I use MVVM and I rarely name my controls. It is pretty evident what they are by context and with the VS designer. Occasionally I will put a comment in the XAML. Jun 14, 2011 at 13:31
I don't know about XAML but for regular old ASP.NET the conventions I've seen are:
- Good old Hungarian (e.g. txtFirstName, ddlState, chkAcceptsTerms)
- Explicit naming (e.g. TextFirstName, DropdownState, CheckAcceptsTerms)
Not sure which I prefer, honestly. I used to see a lot of code like #2 but reversed (e.g. FirstNameTex, StateDropdown, AcceptsTermsCheck) but I like the other way since it groups related controls together.