Solved

mvvm resuse a user control with different data

Posted on 2010-11-12
6
867 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 16

Expert Comment

by:ToddBeaulieu
Comment Utility
what happened?
0
 

Author Comment

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

Author Comment

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

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

772 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now