Search code examples
c#wpfpublic-members

What's the coding convention about public/private members in WPF with binding?


I know this is a largely debated question in stackoverflow (as e.g. in Should I use public or private variables?), but I haven'd been able to find a suitable answer to my doubt.

Shortly said, OO coding etiquette mandates that members of a class be kept private or that getters/setters be used as an alternative.

However, data binding in WPF will not succeed unless the bound class members are public. So, what's the best way out here? Shall I accept declaring public members as an ugly but necessary compromise?


Solution

  • Data binding is normally used on properties, not fields, so that does not contradict the general principle that fields should be kept private.

    The sentence "that members of a class be kept private" is, as such, not correct. Fields should be kept private. The term "members" means fields as well as methods or properties. And of course, it is perfectly sensible to have methods or properties that are public.