Solved

mvvm resuse a user control with different data

Posted on 2010-11-12
6
876 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

 
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

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

762 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