Lukas
Lukas

Reputation: 613

MVVM - static ViewModel

I created some application by using MVVM pattern. Firstly I created couple views (only one was shown) and all of them was using one ViewModel (which contains some List which is used by ListView and other properties)

<UserControl.DataContext>
  <ViewModels:UniversalViewModel />
</UserControl.DataContext>

Now my application needs to show the same data but splitted into few screens. I mean I retrieve for example 12 rows of data per screen, I read from static Config class that I have 2 screens so default main window is opened (it contains some View as Content) and other external window is also opened (it contains the same View but another instance). Cumulatively I retrieve 2*12=24 rows of data and I want to show first 12 rows on first screen, and last 12 rows on second screen (offset).

My idea is to create value converter which will skip x rows of data and retrieve y rows

ItemsSource="{Binding ArrivalDepartures, Converter={.....}}"

but how to identify how many rows need to be skipped (ViewModel is static class so it cannot contain screen-specified data) by converter.

Upvotes: 2

Views: 3566

Answers (3)

adwue
adwue

Reputation: 21

You can keep your ViewModel static - meaning all of the class-variables, properties and methods - if you want to use your class as Datacontext create a "dumb" instance and bind that instance to your view. The Datacontext itself has to be an instance - but it is no problem, when the (bindable) properties of your instance are class-owned/static.

It is up to you, whether you create instances on demand or follow the singleton-pattern and create just one instance (and make it accessible in a static ClassProperty.

In last case the Instance itself is a property of its own - be careful!

Upvotes: 1

ΩmegaMan
ΩmegaMan

Reputation: 31721

Use a MultiBinding Converter (IMultiValueConverter.Convert) and pass in a second parameter the skip offset value of the rows needed per page. The skip value can be passed in / bound to a static value or something on the named page which could provide that value.

<MultiBinding Converter="{converters:SkipRecords}">
   <Binding ArrivalDepartures />
   <Binding ElementName=tbTotalRows Path="Index"/>
</MultiBinding>

Upvotes: 0

Ben Cohen
Ben Cohen

Reputation: 1410

Try set the view-model as static resource, and then DataContext={StaticResource VM}

Upvotes: 0

Related Questions