Question

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.

Était-ce utile?

La 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.

Autres conseils

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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top