Solved

mvvm resuse a user control with different data

Posted on 2010-11-12
6
874 Views
Last Modified: 2012-05-10
I'm new to mvvm in wpf.
I have a user-control (a view) that essentially is a listbox.
The ViewModel shows a wrapped collection of data from the model.
The data is a filtered subset from the ObjectContext of an entity framework model.

Is there any way that I can have a generic ViewModel that can be re-used to bind to copies of the user-control, so that each user control has a different set of items to display?
0
Comment
Question by:quentinA
  • 3
  • 3
6 Comments
 
LVL 16

Accepted Solution

by:
ToddBeaulieu earned 500 total points
ID: 34119711
It's not clear to me if these controls will be in the same view, bound to the same vm or in different views. I'll guess the latter.

There are many ways to accomplish any given challenge.

You could create a base vm that exposes the collection, along with some sort of filter and then base each vm off this base. Internally, each sub classed vm would set the filter.

Alternatively you could have the virtual method(s) inside the base view, allowing each sub classed view to override with their specific code. Which is more appropriate depends on how much custom logic is needed. Heck, the filter itself could even be the virtual method, allowing the sub class to decide if each entity should be included, one at a time.
0
 

Author Comment

by:quentinA
ID: 34120110
Thanks Todd,
I'm not sure if this is allowed in the MVVM paradigm... but is there some way of setting (in the [xaml]view) a property on the ViewModel (which is bound to the view) which, when set, thereby filters the data exposed by the ViewModel to the view.

This presumably would allow me to have several instances of a generic ViewModel that each display different data to similar instances of a generic view (user controls); those instances (views) differing only in what has been set for the filtering property on the bound ViewModel?
0
 
LVL 16

Expert Comment

by:ToddBeaulieu
ID: 34121322
First, OOP (inheritance) is a basic .net concept. You most certainly can create a base vm and extend/override it's members in mvvm.

You can also expose a property just as you described.

There are quite a few ways to skin this.
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 16

Expert Comment

by:ToddBeaulieu
ID: 34121521
what happened?
0
 

Author Comment

by:quentinA
ID: 34121536
I tried to accept your solution, but got an error, how do I award the points to you?
0
 

Author Comment

by:quentinA
ID: 34121566
I tried to accept the solution by ToddBeauLieu but must have made a mistake; please can I try again
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question