views:

299

answers:

2

Hi, I'm working in a WPF project, I'm using the MVVM patter in my project.

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

If I use code behind, obviously there is no problem since I have a direct reference to my control, so I can do "mycontrol.MyMethod();"m, but of course, doing in this way will go against the logic of the MVVM pattern.

I tried to use a Dependency Property in my user control, and use that Dependency Property to bind to it in the xaml of my project but it didn't worked, the compiler says that the property was not found or is not serializable.

So I will appreciate if someone can share some light about how can I accomplish this.

Edited

As far as I understand you have the view, which is all the GUI, then you have the model, which is all the logic, and them you have the view-model which is like an intermediate layer used to bind the view with the model, right?

In this way I have developed my project, however I came to the problem that I need a custom control, a TextBox that remember what the user entered, and when he start typing, if there are words that start with that letter, those words are shown as a suggestion, as Google does it.

This TextBox is used as a search filter; so I created a user control to do this, I added a method to my user control to allow whatever application that uses my control to add items to an internal array that holds all the strings that the user has entered.

I created a user control because I couldn't 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 someway be able to call the method that add the items to the internal array, but maybe I'm doing things the wrong way, so if any of you has a better idea, I will appreciate if you shared it with me.

A: 

You shouldn't call something in the View from the ViewModel since that breaks the model.

If the reason you want to call the method in the user control is to do with UI only, I don't see anything wrong with doing it from the view - the view's cs and the view's xaml are in the same "space" in the model. You can be overly-purist in wanting to have lean and mean view cs files.

amaca
well I said that I want to call the method from the ViewModel because is the closest to the view, but you are right, following the model logic, I should call the method from the model, however, the problem still remains, how do you do to call the method inside the user control from the model? the method that I need to use add items to an internal array in the user control...
Vic
@Vic I think you went the other way with Amaca's suggestion. When Amaca said "Model", he/she was saying the *MVVM pattern*. You should never *ever* need to refer directly to the View from the Model. Perhaps you can edit your answer with more details about your scenario and someone might be able to suggest something to you. It's likely that you created a control when you didn't have to (creating an actual Control should be pretty rare) and we can help you sort this out a little more
Anderson Imes
thanks, I added more info to my question, hope this helps.
Vic
A: 

Why does the consumer of the user control need to maintain the control's internal array? That seems like you've exposed an implementation detail that you don't need to.

Why not simply make that array a dependency property (and an IEnumerable<string> or ObservableCollection<string> besides)? Then you can simply create the corresponding property in your view model and bind it to the control. It also makes the control considerably more versatile.

Robert Rossney
yeah, you are totally right, in that way is easier, however, I try it just as you suggested me but the dependency property in my user control is never updated when I add or remove an item in the corresponding property on the view model.
Vic
@Vic: that sounds like either an INotifyPropertyChanged issue or something similar. That's basic binding notification stuff. Robert's suggestion should work... you're just going to have to dig a little on the binding. This article will help you debug bindings: http://bea.stollnitz.com/blog/?p=52
Anderson Imes
I found two ways to solve this problem, the first one was to make my target property a Dependency Property, and thus the View-Model class will have to subclass from DependencyObject. The other way was to have my target property as a common property, but in the set{} you have to add the line "base.RaisePropertyChanged("your_property_name");" and the View-Model class will have to subclass from ObservableObject. Thanks a lot because without your comments I wouldn't make it.
Vic