Print Page | Close Window

Viewmodel and filtered items

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce 2010
Forum Discription: For .NET 4.0
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=3481
Printed Date: 13-May-2026 at 5:47am


Topic: Viewmodel and filtered items
Posted By: pponzano
Subject: Viewmodel and filtered items
Date Posted: 05-Jun-2012 at 4:21am
Hello,
I'm on a silverlight application MVVM pattern,
I've a viewmodel that has

DateTime SelectionStart
DateTime SelectionEnd

I've also an item Indicator in this viewmodel that's

Int ID
string Description
[...]
IEnumerable<SNAPSHOT> Snapshots


SNAPSHOT in the meanwhile has

int ID
double VALUE
datetime DT_TS

I've 2 view that one displays the data in a timebar and the same data are represented on a gridview in the second usercontrol

I wish to filter on my gridview just with the item that're in the range SelectionStart < DT_TS < SelectionEnd...

since I can't bind a method to xaml and I can't have the selectionStart/End in the SNAPSHOT item or in the Indicator, what can I do?
Thanks



Replies:
Posted By: mgood
Date Posted: 05-Jun-2012 at 8:50am
Since you loaded all the snapshots, you can filter on the client like so:

public DateTime SelectionStart
{
    get { return _selectionStart; }
    set {
               _selectionStart = value;
               NotifyOfPropertyChanged(() => SelectionStart);
               NotifyOfPropertyChanged(() => FilteredSnapshots);
    }
}

public DateTime SelectionEnd
{
    get { return _selectionEnd; }
    set {
               _selectionStart = value;
               NotifyOfPropertyChanged(() => SelectionEnd);
               NotifyOfPropertyChanged(() => FilteredSnapshots);
    }
}

public IEnumerable<SNAPSHOT> FilteredSnapshots
{
    get { return Snapshots.Where(x => _selectionStart < x.DT_TS < _selectionEnd); }
}

Then bind the Grid to FilteredSnapshots and whenever SelectionStart or SelectionEnd changes it triggers a refresh of the grid by raising a PropertyChanged on FilteredSnapshots.



Posted By: pponzano
Date Posted: 05-Jun-2012 at 8:59am
Hello Marcel,
I load the snapshots related to a particular indicator (via .Include(x=>x.SNAPSHOT)) .... those property should be put inside a partial class that extends my Indicator?


Posted By: mgood
Date Posted: 05-Jun-2012 at 9:27am
I'm a bit confused. What are you showing in the grid? Indicatori or Snapshots? If it's Indicatori, then just change the FilteredSnapshots accordingly. No partial classes necessary, just use the power of LINQ. You can query any in-memory collection, much like you query a database thanks to "LINQ to Objects".

public IEnumerable<INDICATORI> FilteredIndicatori
{
    get { return Indicatori.Where(x => x.Snapshots.Any(y => _selectionStart < y.DT_TS < _selectionEnd)); }
}



Print Page | Close Window