WPF: Binding with nonstatic parameter? (newbie question)

757 views Asked by At

This will probably be obvious but I can't find the best way.

I want to show the user's ToDo's in a listbox. These ToDo's are in the database and consist of an Id, UserId and Description.

The user logged in to the app.

How can I retrieve the ToDo's for that certain userId and set it up for binding to the listbox?

I was trying with an ObjectDataProvider but I cant figure out how to use that in combination with nonstatic stuff (like my _dbService, userId, language, ...).

Are the only options to make all those things static versus binding in the code behind?

If so, this means that ObjectDataProvider isn't very useful, no? I find a lot of examples of it being used with a hardcoded parameter but I hardly see any situation where I'd need such a functionality..

2

There are 2 answers

0
serialhobbyist On BEST ANSWER

I do all my WPF using the Model-View-ViewModel pattern. I've given you one link there but Google will give you loads. MVVM seems to be the standard pattern for WPF. This project is probably more complicated than you need but it is well-written and brings home the use of MVVM.

Basically, you create a Model of your data. In this case, you'd probably create a simple class (I'll call it ToDoItem) with properties Id, UserID and Description. Use your preferred mechanism to get a collection of these from the database. Link to SQL, Entity Framework, a standard query, whatever.

Then you have your ViewModel - you have an instance of the ViewModel for each instance of the Model: the VM has a reference to the M and 'forwards' properties to it. The ViewModel is what you use to manipulate the model.

Then you have your View - this is the UI. You set the DataContext of the View to be the ViewModel and then your bindings automatically bind to the ViewModel. Your View just ends up being the things you can see. All of the work gets done in the ViewModel. This means it's very easy to test.

So, when you click on a button in your View, the bindings pass this onto a Command in your ViewModel which manipulates the Model.

The UI is also a View with a ViewModel. So, your UI VM might load a collection of Models from the database and stick them in an ObservableCollection. The ListBox items collection would be bound to this ObservableCollection.

It's hard to explain all of this in a post like this. Read a couple of articles and see what you think. I'm still quite new at this, too, but I believe my reading about MVVM has paid off.

0
buckley On

Hela Thomas, Tom here from Orbit One :)

MVVM is the way to go. I'm on my 4th project and WPF really shines if you use mvvm. You already tried MVC (or MVP as we did on recy*tyre) and that's a nice separation of concern. MVVM takes it a step further since the viewmodel knows absolutely nothing about the view.

The view binds to the viewmodel, so it has a reference to it (2 way, super powerful and works beyond the typical MS demo). The viewmodel is just a poco and is a representation of your view, data + behaviour. Once you dig this paragraph the cool term mvvm will have no secrets.

I see if I can come up with a small demo. Maybe I'll have time later. What I will come up with is a view (xaml, file 1) that binds to a viewmodel (file 2, a poco class, not to be mistaken with code behind). The model can be whatever you like (service layer or directly to the repositories). Using the power of 2 way binding we will bind to an observable collection meaning that if we add/delete/... something to the collection the view will pick it up without us putting energy into it.

My first 2 wpf projects was done with Caliburn Micro (see codeplex) which is a powerful framework based on conventions. It shields you away from hardcore wpf (creating tour dependency properties yourself mainly) and you can create something relatively fast without fully understanding wpf. That's a downside of itself but it worked for me. As of project 3 I started taming those dependency properties myself and it will make you a better wpf developer.

I see the question is from October.. did you find a good solution?