I’ve been using mvvm to develop a RIA service sl4 app and i seem to be missing something.
The MVVM and databinding works nice when your data comes in the expected format for editing or when your data objects “fits the view” (grids, lists, etc). But what happens when your data doesn’t really map directly?
My example
Lets say i have a product table, this defines the product its price and options. And i have a subscribed product table that will link the product and client and also have data on when the subscription ends etc…
So when i started working on my view for a “shopping list” i did this:
<ListBox x:Name="ShopList" Grid.Row="0" ItemsSource="{Binding Products}">
<DataTemplate>
<StackPanel Orientation="Horizontal">
<sdk:Label Content="{Binding ModuleName}" />
<sdk:Label Content="{Binding DateStart, Converter={StaticResource gridDateFormatter}}" />
<sdk:Label Content="{Binding DateEnd, Converter={StaticResource gridDateFormatter}}" />
<telerik:RadMaskedTextBox MaskedText="{Binding UnitsToBuy}" />
<sdk:Label Content="{Binding UnitStep}" />
<sdk:Label Content="{Binding TotalPrice}" />
</StackPanel>
</DataTemplate>
</ListBox>
So i thought well I’m gonna bind ItemsSource with a Observable collection on my ViewModel
public ObservableCollection<Product> Products
But now i have a problem, the UnitsToBuy is something not in the Product, and doesn’t belong on the product. I’m strugling on how to find a clean way to deal with these kinds of scenario. Do assume i can have any number of items in that list.
Thanks.
I think what you’re asking boils down to the fact that the view needs a viewmodel with more properties, not a simple model. Why not have something like this. Obviously the classes below should implement INotifyPropertyChanged.
and then in xaml you don’t really need to make any changes.