I am trying some different things using MVVM. In our ViewModel properties which are bind to View are public. I am taking example of a button binding. Here is a simple sample.
View.xaml:
<Button Content="Test Button" Command="{Binding TestButtonCommand}" />
ViewModel.cs
private ICommand _testButtonCommand;
public ICommand TestButtonCommand
{
get { return _testButtonCommand?? (_testButtonCommand= new RelayCommand(SomeMethod)); }
}
Here my question is that can we make TestButtonCommand
internal instead of public? Internal means it is accessible to current project so their should not be any problem doing that? But when I tried to do that it didn't worked. Adding a breakpoint in getter was not hit. So why we cannot make it internal?
Here is the link from msdn.
http://msdn.microsoft.com/en-us/library/ms743643.aspx
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.
Why we cannot do this?
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.
internal ICommand TestButtonCommand { ...... }
Binding paths address the different properties and lists in a model and define how a node in the hierarchical data tree can be found. A binding path consists of a number of name tokens, which are separated by a separator char. In all models provided by the framework, the separator char is the slash "/".
Data binding is a mechanism in XAML applications that provides a simple and easy way for Windows Runtime Apps using partial classes to display and interact with data. The management of data is entirely separated from the way the data is displayed in this mechanism.
A binding source is usually a property on an object so you need to provide both the data source object and the data source property in your binding XAML. In the above example the ElementName attribute signifies that you want data from another element on the page and the Path signifies the appropriate property.
Data binding in Windows Presentation Foundation (WPF) provides a simple and consistent way for apps to present and interact with data. Elements can be bound to data from different kinds of data sources in the form of . NET objects and XML.
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
.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With