We are folllowing mvvm approach for a wpf application.
We have are following view-model approach..I mean we create view-models and map them using
<DataTemplate Dat开发者_运维技巧aType="{x:Type vm:CityViewModel}">
<vw:Cities/>
</DataTemplate>
In this city - view ..I have a user control...which I am using multiple times...
<view:UserControl1 Grid.Row="2" DataContext="{Binding UcViewModel}" Margin="291,5,291,-5"></view:UserControl1>
<view:UserControl1 Grid.Row="3" DataContext="{Binding Uc2ViewModel}" ></view:UserControl1>
We create multple instances of user control view model inside CityViewmodel.
Does this approach comply with mvvm ???
I would consider the MVVM pattern to be a loose guide.
Ideally what you are looking for is a testable application. Any code in the UI is harder to test.
If this works in you circumstances then go for it, but keep testability in mind.
In an application I am working on at the moment I have an ItemsControl with 6 instances of the same UserControl and ViewModel.
Edit:
public class InsuranceViewModel
{
public ObservableCollection<UnderwritingViewModel> Underwriting { get; set; }
}
In the view I have:
<ItemsControl ItemsSource="{Binding Path=Underwriting}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<!-- this could be another UserControl -->
<views:UWView DataContext="{Binding}" />
<!-- or a full data template defined in this view -->
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
Or you could put this in your resources:
So, in effect, all you have to do is create new ViewModel instances in your DataContext and the template will take care of the View creation.
精彩评论