Solved

mvvm resuse a user control with different data

Posted on 2010-11-12
6
879 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
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
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

739 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