Creating a binding property internal rather than open in ViewModel

I am trying to use different things using MVVM. In our ViewModel properties that are bound to the View are publicly available. I take an example of button binding. Here is a simple example.

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's my question: can we make TestButtonCommand internal, not public? Internal means that it is available for the current project, so shouldn't they be a problem? But when I tried to do this, it did not work. Adding a breakpoint to the getter has not been removed. So why can't we make this internal.

Here is the link from msdn.

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

The properties that you use as properties of the binding source for the binding must be public properties of your class. Explicit interface properties cannot be accessed for binding purposes, and private, internal, or virtual properties that do not have a basic implementation cannot be protected.

Why can't we do this? In the case of access, the internal one is the same as public, if it works in the same project. Then why can't we use the internal ones here. There must be a reason that they should be publicly available, and I'm looking for this reason.

 internal ICommand TestButtonCommand { ...... } 
+6
source share
6 answers

In the case of internal access, it is the same as public, if it works in the same project. Then why can't we use the internal ones here. There must be a reason that they should be publicly available, and I am looking for this reason.

You have an answer in your question only because internal elements can only be accessed inside one assembly, and not outside. This is the only reason that snapping to internal components does not work, because snapping is allowed using the snapping mechanism, not your assembly and it is in a separate assembly of PresentationFramework.dll , to be precise, if you are looking for it.

+11
source

Binding is only supported for public properties. MSDN Link:

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

As indicated in the link

The properties that you use as properties of the binding source for the binding must be public properties of your class. The explicit interface of the property cannot be accessed for binding purposes and cannot be protected by private, internal, or virtual properties that do not have a base implementation.

+7
source

Obviously, it depends on what you are trying to achieve from this situation - you do not indicate what the common goal is. I just ran into a similar problem with my code, and also came up with a solution for my case. One of my libraries contains auxiliary objects with various properties, but when they are used in the application project, I want to see only those properties that suit me - I wanted to hide, for example, Command properties.

My decision to hide them from the "user" of the library is to add

 <EditorBrowsable(EditorBrowsableState.Never)> 

for every property that interests me little or not at all.

Hope this helps someone!

+2
source

Internal visibility really only makes sense to the IL compiler and verifier, since they know the full context of member access; WPF binding mechanism does not work. He knows that there is a binding to a property; he does not know who set the property. It could be installed in XAML or dynamically at runtime (technically, even if you installed it in XAML, it is still applied dynamically).

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

+1
source

The created internal property breaks the good OO design and breaks the encapsulation. You can use the internal access set (and public access to it) for your case.

 public ICommand SaveCommand { get; internal set; } 

If you have a field encapsulated in a property, you must make this rule to always access this field through the property, even inside your class. This is the best practice.

0
source

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

For CLR properties, data binding works as long as the binding mechanism can access the binding source property using reflection. Otherwise, the binding mechanism issues a warning that the property cannot be found and uses the return value or the default value, if available.

0
source

Source: https://habr.com/ru/post/957112/


All Articles