我宣布了一个ViewModel
:
public class DefaultViewModel : WorkspaceViewModel
{
public DefaultViewModel()
{
this.DisplayName = "Welcome!";
}
}
我利用CollectionViewSource
设置我的活动“工作区”:
// this code comes from the MainWindowViewModel.cs
void SetActiveWorkspace(WorkspaceViewModel workspace)
{
Debug.Assert(this.Workspaces.Contains(workspace));
ICollectionView collectionView =
CollectionViewSource.GetDefaultView(this.Workspaces);
if (collectionView != null)
collectionView.MoveCurrentTo(workspace);
}
并在MainWindowViewModel.cs
我的构造函数中设置了一个默认的“工作区”:
public MainWindowViewModel()
{
this.DisplayName = "Big File Reader";
var viewModel = new DefaultViewModel();
this.Workspaces.Add(viewModel);
this.SetActiveWorkspace(viewModel);
}
在这一点上一切都应该很好。现在,我想在新选项卡中显示每个“工作区”,所以我标记TabControl
并同步了它:
<ContentControl Content="{Binding Path=Workspaces}">
<ContentControl.ContentTemplate>
<DataTemplate>
<TabControl IsSynchronizedWithCurrentItem="True"
ItemsSource="{Binding}"
Margin="4">
<TabControl.ItemTemplate>
<DataTemplate>
<DockPanel Width="120">
<Button Command="{Binding Path=CloseCommand}"
Content="X"
Cursor="Hand"
DockPanel.Dock="Right"
Focusable="False"
FontFamily="Courier"
FontSize="10"
FontWeight="Bold"
Margin="0,1,0,0"
Padding="4"
VerticalContentAlignment="Bottom"
Style="{DynamicResource
ResourceKey={
x:Static ToolBar.ButtonStyleKey}}"/>
<ContentPresenter
Content="{Binding Path=DisplayName}"
VerticalAlignment="Center"/>
</DockPanel>
</DataTemplate>
</TabControl.ItemTemplate>
</TabControl>
</DataTemplate>
</ContentControl.ContentTemplate>
</ContentControl>
然后,在一个外部资源文件中,我为视图模型定义了默认视图:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:l="clr-namespace:BigFileReader"
xmlns:lv="clr-namespace:BigFileReader.Views">
<DataTemplate DataType="l:DefaultViewModel">
<lv:DefaultView/>
</DataTemplate>
</ResourceDictionary>
我在主窗口中包含了该资源字典:
<Window.Resources>
<ResourceDictionary Source="MainWindowResources.xaml" />
</Window.Resources>
现在,标题为每个TabItem
. 它DisplayName
按预期显示。
但是,ContentTemplate
forTabItem
并没有选择默认视图,它只是显示TextBlock
带有 的ToString()
,DefaultViewModel
这当然是类型的全名。
为什么没有选择默认模板?