【发布时间】:2011-08-30 12:59:21
【问题描述】:
我一直在使用 mvvm 开发 RIA 服务 sl4 应用程序,但我似乎遗漏了一些东西。
当您的数据以预期的编辑格式出现或当您的数据对象“适合视图”(网格、列表等)时,MVVM 和数据绑定工作得很好。但是当您的数据没有真正直接映射时会发生什么?
我的例子
假设我有一个产品表,它定义了产品的价格和选项。而且我有一个订阅的产品表,它将链接产品和客户,并且还有关于订阅何时结束等的数据......
所以当我开始为“购物清单”设计视图时,我这样做了:
<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>
所以我想我会在我的 ViewModel 上将 ItemsSource 与 Observable 集合绑定
public ObservableCollection<Product> Products
但现在我有一个问题,UnitsToBuy 不在产品中,也不属于产品。我正在努力寻找一种干净的方法来处理这些情况。假设我可以在该列表中包含任意数量的项目。
谢谢。
【问题讨论】:
-
UnitsToBuy 是另一个模型对象的一部分,还是它本身?我想我的问题是您是否要尝试将两个模型合并在一起。
-
@avanek 好吧,它将用于另一个模型,比如说 SubscribedProduct 模型/表
标签: xaml silverlight-4.0 mvvm