Solved

mvvm resuse a user control with different data

Posted on 2010-11-12
6
875 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C# Gridview 1 45
VB.NET 2008 - SQL Timeout 9 34
Convert datetime to time string 10 28
Formula for calculating ROI on training 6 49
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

792 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