How to call a user management method using MVVM?

I am working in a WPF project, I am using MVVM patter in my project.

I created a user control (also in WPF) and I want to use it in my project, now the problem is that I have a method in my user control that I need to call from my view model, but I don’t I know how to do this, how to bind to a method inside my control from a view model.

If I use the code behind, it is obvious that there is no problem, as I have a direct link to my control, so I can do "mycontrol.MyMethod ();" m, but of course this will go against the logic of the MVVM pattern.

I tried to use the Dependency property in my user control and use the Dependency property to bind to it in the xaml of my project, but it did not work, the compiler says that the property was not found or is not serializable.

So, I would be grateful if someone could tell a little about how I can do this.

Edited

As far as I understand, you have a view that represents the entire graphical interface, then you have a model that is all the logic, and you have a view model that looks like an intermediate layer used to bind the view to the model, right?

Thus, I developed my project, however, I came to the problem that I need a custom control, a text block that remembers what the user entered, and when it starts typing, if there are words starting with this letter, these words are shown as an offer like Google does.

This TextBox is used as a search filter; so I created a user control for this, I added a method for my user control so that any application using my control would add elements to the internal array that contains all the rows that the user entered.

I created a user control because I could not find any control that behaves the way I want.

So, my problem is when I add my user control to the main project, because I need to somehow call a method that adds the elements to the internal array, but maybe I'm doing something wrong if one of you has a better idea, I would be grateful if you shared it with me.

+4
source share
3 answers

Why does the user of the user control need to maintain an internal control array? It looks like you have revealed implementation details that you do not need.

Why not just make this array a dependent property (and IEnumerable<string> or ObservableCollection<string> )? Then you can simply create the corresponding property in your view model and bind it to the control. It also makes management significantly more versatile.

0
source

You should never call View methods from ViewModel and vice versa.

Create a property (ObservableCollection?) On the ViewModel, it will have a CollectionChanged event, subscribe to it to track changes (if necessary).

When you add an item to the collection in the ViewModel, the GUI will update accordingly (you need to perform the Add () operation in the GUI thread, btw).

If you need to change the current position in your list, there is a common use for this (CollectionViewSource, etc.).

If you really need to pass the string under your control, create a DependencyProperty and bind its OneWay to the ViewModel property. When you set the value, it will call PropertyChangedCallback on your DependencyProperty.

+1
source

You should not call something in the ViewModel, as this violates the model.

If the reason you want to call the method in the user control is to do it only with the user interface, I see nothing wrong with doing this from the view - the cs view and xaml view are in the same space "in models. You may be too purist, wanting to have meager and average cs view view files.

0
source

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


All Articles