让 wpf tabcontrol 延缓初始化每个tab item content
当手动增加tabItem给tab control时, 尽管只是显示第一个tab item的content, 但是每个tab item的content的都会被创建,这样会使得创建tab control时比较慢,特别是tab item很多,元素也很多的时候;,(这种情况下itemsControl 的UI virtualization也是失效的),
<
TabControl x:Name="TabControl"><TabItemx:Name="DetailTabItem" Header="Detail">
<local:DetailTabView x:Name="DetailTabView"DataContext="{Binding Path=Detail}" /></TabItem>
<TabItemx:Name="DetailTabItem2" Header="Detail2">
<local:DetailTabView x:Name="DetailTabView2" DataContext="{Binding Path=Detail2}" />
</TabItem>
</TabControl>
创建DetailView比较好时
给custom tabItem control加上ContentType和 ContentDataContext 两个属性,当TabControl的 selectionChanged时候再去创建content
<TabControl x:Name="TabControl">
<TabItemx:Name="DetailTabItem" Header="Detail"
ContentType="{x:Type local:DetailTab}"
ContentDataContext="Detail"
> </TabItem>
</TabControl>
然后在tabcontrol的selection change时候创建content
protectedoverridevoid OnSelectionChanged(System.Windows.Controls.SelectionChangedEventArgs e)
{
MyTabItem vti = selectedTabItem asMyTabItem;
if (null == vti.Content)
{
vti.Content = Activator.CreateInstance(vti.ContentType);
if (null != vti.ContentDataContext)
{
Binding binding = newBinding();
binding.Source = this.DataContext;
binding.Path = newPropertyPath(vti.ContentDataContext);
((FrameworkElement)vti).SetBinding(FrameworkElement.DataContextProperty, binding);
}
这样就把tab item content初始化延迟到tab control 得selection change时候创建,提高初始化tab control的速度。虽然增加了每个tab第一次打开的时间。