【发布时间】:2014-12-16 21:21:33
【问题描述】:
我对以下代码示例的工作方式感到非常困惑。似乎 Contentcontrol 从窗口资源中获取数据模板。
contentcontrol 没有自己的“内容模板”来确定其中的视图吗?如果没有设置任何内容,它是否只是向上搜索并将其内容设置为数据模板?
我可能混淆了这两个术语,但我真的很困惑为什么该示例将数据模板存储在窗口资源级别。
无论如何,我想了解我有哪些选项可以为给定窗口设置视图。最初我打算只使用一个内容控件并在其中托管视图,但后来我想知道是否可能有理由让内容控件选择一个页面(从实际页面类)而不是一个视图(扩展用户控制)。
<Window.Resources>
<DataTemplate DataType="{x:Type local:HomeViewModel}">
<local:HomeView />
</DataTemplate>
<DataTemplate DataType="{x:Type local:ProductsViewModel}">
<local:ProductsView />
</DataTemplate>
</Window.Resources>
<DockPanel>
<Border DockPanel.Dock="Left" BorderBrush="Black" BorderThickness="0,0,1,0">
<ItemsControl ItemsSource="{Binding PageViewModels}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button Content="{Binding Name}"
Command="{Binding DataContext.ChangePageCommand, RelativeSource={RelativeSource AncestorType={x:Type Window}}}"
CommandParameter="{Binding }"
Margin="2,5"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Border>
<ContentControl Content="{Binding CurrentPageViewModel}" />
</DockPanel>
【问题讨论】:
标签: wpf xaml mvvm navigation