[英]Lazy loading in TabControl WPF MVVM (yet, again)
I know, that this topic has been discussed many times , so straight to the point. 我知道,这个话题已经讨论了很多次 ,直截了当。
This is the ItemsSource
of TabControl
: 这是
TabControl
的ItemsSource
:
Tabs = new ObservableCollection<aTabViewModel>
{
new HomeViewModel(),
new StatisticsViewModel()
};
Here is the TabControl
itself 这是
TabControl
本身
<TabControl ItemsSource="{Binding Tabs}">
<TabControl.Resources>
<DataTemplate DataType="{x:Type pagesVM:HomeViewModel}">
<pages:Home/>
</DataTemplate>
<DataTemplate DataType="{x:Type pagesVM:StatisticsViewModel}">
<pages:Statistics/>
</DataTemplate>
</TabControl.Resources>
<TabControl.ItemContainerStyle>
<Style BasedOn="{StaticResource MetroTabItem}" TargetType="TabItem">
<Setter Property="Header" Value="{Binding Header}" />
</Style>
</TabControl.ItemContainerStyle>
HomeViewModel
and StatisticsViewModel
are instantiated first time when we add them to Tabs
collection, and second time when we select tab in a application (which is seems to be a behaviour of TabControl
). 第一次将
HomeViewModel
和StatisticsViewModel
实例化时,将它们添加到Tabs
集合中,而第二次当我们在应用程序中选择tab时被实例化(这似乎是TabControl
的行为)。 And this double-loading apparently isn't right thing. 而且这种双重加载显然不是正确的事情。
Q: How can I make my tabs load only when selected? 问:如何使我的标签页仅在选中时加载?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.