Why variables are not allowed for binding

224 views Asked by At

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.

2

There are 2 answers

0
Botz3000 On BEST ANSWER

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.

0
Tigran On

Well, it's not completely true. You can use a field for a binding, but first you have to declare it like a resource and after access it via resource key in binding declaration.

When you're going to bind to code binding to a property is done for be able to iject also OnPorpertyChanged notification too.