如何实现懒在WPF选项卡页面控件绑定? [英] How to Achieve Lazy Binding of Tab Page Controls in WPF?
问题描述
我有一个实体类。这个实体有很多属性和实体的数据显示给用户一些的的TabControl
的TabItems
。我也实现MVVM的做法。
I have an entity class. This entity has lots of properties and entity's data is shown to the user in several TabItems
of a TabControl
. I also implement MVVM approach.
当屏幕显示给用户第一,我要绑定仅活动标签页控制和用户浏览的标签页视需要额外单独的绑定将发生。我怎样才能做到这一点?
When the screen is shown to the user first, I want to bind only the active tab page controls and as the user navigates through tab pages additional separate bindings will be incurred as-needed. How can I achieve that?
推荐答案
您不要有什么做的,这是默认行为。在的DataTemplate
为 TabItem的
内容不会被实例化直到 TabItem的
选择
You don't have anything to do, that's the default behavior. The DataTemplate
for a TabItem
content won't be instantiated until this TabItem
is selected
编辑:这里有一个例子:
here's an example:
<Window.Resources>
<DataTemplate DataType="{x:Type vm:Page1ViewModel}">
<v:Page1View />
</DataTemplate>
<DataTemplate DataType="{x:Type vm:Page3ViewModel}">
<v:Page3View />
</DataTemplate>
<DataTemplate DataType="{x:Type vm:Page3ViewModel}">
<v:Page3View />
</DataTemplate>
</Window.Resources>
...
<TabControl ItemsSource="{Binding Pages}"
DisplayMemberPath="Title">
</TabControl>
在上面的code时,的TabControl
将选择适当的的DataTemplate
根据项目类型,并会使其仅在选择该项目。
In the code above, the TabControl
will pick the appropriate DataTemplate
based on the item type, and will render it only when that item is selected.
编辑2:显然你希望显示多个页面的单一视图模型的数据。如果您希望每个 TabItem的
懒洋洋地实例化的控件,您需要使用每个<$ C的的ContentTemplate
属性$ C> TabItem的:
EDIT 2: apparently you want to display the data of a single ViewModel on several pages. If you want the controls of each TabItem
to lazily instantiated, you need to use the ContentTemplate
property of each TabItem
:
<TabControl>
<TabItem Header="Page 1">
<TabItem.ContentTemplate>
<DataTemplate>
<v:Page1View />
</DataTemplate>
</TabItem.ContentTemplate>
</TabItem>
<TabItem Header="Page 2">
<TabItem.ContentTemplate>
<DataTemplate>
<v:Page2View />
</DataTemplate>
</TabItem.ContentTemplate>
</TabItem>
<TabItem Header="Page 3">
<TabItem.ContentTemplate>
<DataTemplate>
<v:Page3View />
</DataTemplate>
</TabItem.ContentTemplate>
</TabItem>
</TabControl>
这篇关于如何实现懒在WPF选项卡页面控件绑定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!