views:

124

answers:

1

Hi guys,

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..

+1  A: 

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.

serialhobbyist
thx, will probably switch to this
Thomas Stock