Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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?

share|improve this question
1  
What ever scheme you choose - be consistent across your application. –  ChrisF Nov 25 '10 at 17:20
add comment

3 Answers

up vote 4 down vote accepted

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.

share|improve this answer
    
Those are guidelines for naming members in libraries, not UI elements. –  Robert Harvey Nov 25 '10 at 19:54
    
@Robert - good point. I hadn't noticed that their guideline excluded UI elements. I'll edit my answer. –  Walter Nov 25 '10 at 21:40
add comment

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

share|improve this answer
    
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. –  Robert Harvey Nov 25 '10 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. –  Matthieu Nov 25 '10 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. –  M. Dudley Jun 14 '11 at 13:31
add comment

I don't know about XAML but for regular old ASP.NET the conventions I've seen are:

  1. Good old Hungarian (e.g. txtFirstName, ddlState, chkAcceptsTerms)
  2. 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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.