Search code examples
c#wpflanguage-concepts

Why variables are not allowed for binding


I was googling for this point where why the local or global variables are not allowed as sources for WPF data binding; only the wrapping property can be bound to.

So the question is where properties are the wrapper over the variables then why these properties are allowed and the variables are not allowed.

I need to know under the hood system.


Solution

  • The mechanisms used for binding (PropertyDescriptor, DependencyProperties etc.) only use properties, that's the reason you can't bind to fields. Properties can provide validation, change notification and more, which is another reason to prefer properties. Fields provide none of these.

    Also, using public fields is usually bad practice, so why should they bother implementing binding to fields? It would only promote those bad practices.