New Posts New Posts RSS Feed: Viewmodel and filtered items
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

Viewmodel and filtered items

 Post Reply Post Reply
Author
pponzano View Drop Down
Senior Member
Senior Member
Avatar

Joined: 28-Apr-2011
Location: Italy
Posts: 165
Post Options Post Options   Quote pponzano Quote  Post ReplyReply Direct Link To This Post Topic: Viewmodel and filtered items
    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
Back to Top
mgood View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 18-Nov-2010
Location: Emeryville, CA
Posts: 583
Post Options Post Options   Quote mgood Quote  Post ReplyReply Direct Link To This Post 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.

Back to Top
pponzano View Drop Down
Senior Member
Senior Member
Avatar

Joined: 28-Apr-2011
Location: Italy
Posts: 165
Post Options Post Options   Quote pponzano Quote  Post ReplyReply Direct Link To This Post 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?
Back to Top
mgood View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 18-Nov-2010
Location: Emeryville, CA
Posts: 583
Post Options Post Options   Quote mgood Quote  Post ReplyReply Direct Link To This Post 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)); }
}
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down