【发布时间】:2011-02-09 07:28:38
【问题描述】:
所以在我见过的经典 MVVM 示例中,DataTemplate 定义用于将视图模型映射到视图,在 MVVM Light 框架中执行此操作的标准方法是什么,映射应该位于哪里?以下是我现在正在做的和我正在谈论的示例,可混合性对我来说很重要!
主窗口:
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
x:Class="STS2Editor.MainWindow"
Title="{Binding ApplicationTitle, Mode=OneWay}"
DataContext="{Binding RootViewModel, Source={StaticResource Locator}}">
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Skins/ApplicationSkin.xaml" />
<ResourceDictionary Source="Resources/ViewMappings.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
<Grid>
<ContentControl Content="{Binding ApplicationManagementViewModel}" HorizontalAlignment="Left" VerticalAlignment="Top"/>
</Grid>
</Window>
在上面的代码中,我的 RootViewModel 类有一个具有相同属性名称的 ApplicationManagementViewModel 类的实例:
public ApplicationManagementViewModel ApplicationManagementViewModel {get {...} set {...} }
我引用 ResourceDictionary“ViewMappings.xaml”来指定我的视图模型如何表示为视图。
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:STS2Editor.ViewModel">
<DataTemplate DataType="{x:Type local:ApplicationManagementViewModel}">
<local:ApplicationManagementView/>
</DataTemplate>
</ResourceDictionary>
我应该使用 ViewModelLocator 做这样的事情吗?视图模型的集合呢?
【问题讨论】:
-
我意识到 MVVM 的一大问题是缺乏统一的方法。
-
我认为这不是 MVVM 本身的问题。这是一种模式,具有多种实现。与 MVC 或其他相同。我不认为这是一个问题,这实际上取决于您要解决的情况和问题。
-
我有非常相似的问题。那么 DataTemplates 对 WPF 来说足够好了吗?我想要一个 Prism 区域类型的东西,但没有 Prism。
标签: wpf mvvm mvvm-light blendability