Binding to an internal property?

Binding is only supported for public properties. MSDN reference:

http://msdn.microsoft.com/en-us/library/ms743643.aspx

As quoted in the reference

The properties you use as binding source properties for a binding must be public properties of your class. Explicitly defined interface properties cannot be accessed for binding purposes, nor can protected, private, internal, or virtual properties that have no base implementation.


The internal visibility is really only meaningful to the compiler and the IL verifier, because they know the full context of the member access; the WPF binding engine does not. It knows that a binding exists on a property; it has no idea who set the property. It could have been set in the XAML, or dynamically at runtime (technically, even if you set it in the XAML, it is still applied dynamically).

Since there is no way to enforce the access rules, allowing binding to internal properties would be equivalent to allowing binding to private properties, not public properties.


Obviously, it depends on what you're trying to achieve from this situation - you don't state what the overall aim is. I have just come across a similar problem with my code, and also happened upon a solution for my case. One of my libraries contains helper objects with various properties, but when these are used in the application project, I only want to see the properties that are of any use to me - I wanted to hide, for example, the Command properties.

My solution to hide them from the 'user' of the library is to add the

<EditorBrowsable(EditorBrowsableState.Never)>

attribute to each property that bears little or no interest to me.

Hope that helps someone!


In case of access internal is same as public if working in the same project. Then why we cannot use internal here. There must be a reason that these should be public, and I am looking for that reason.

You have part of your answer in your question itself in the quote from Microsoft:

The properties you use as binding source properties for a binding must be public properties of your class.

Presumably / speculatively, the reason for this is that internals can only be accessed within the same assembly and not from outside. Binding to internals doesn't work because binding is resolved by the WPF binding engine which is in a separate assembly PresentationFramework.dll.

Tags:

C#

Wpf

Binding

Mvvm