为什么选项卡控件在更改选项卡时重用 View 实例 [英] Why do tab controls reuse View instances when changing tab

查看:17
本文介绍了为什么选项卡控件在更改选项卡时重用 View 实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个遵循 MVVM 模式的 WPF 项目.

I have a WPF project following an MVVM pattern.

我有一个包含选项卡控件的控件.选项卡控件绑定到一个可观察的集合.observable 集合中的每一项都是要显示的特定标签页的视图模型.

I have one control that contains a tab control. The tab control binds to an observable collection. Each item in the observable collection is a view model for a specific tab page to be shown.

实现 tab 控件的 xaml 文件使用数据模板来选择要显示的特定用户控件,具体取决于可观察集合中视图模型的具体类型.

The xaml file implementing the tab control uses a data template to select a specific user control to display, depending on the concrete type of the view model in the observable collection.

我观察到的奇怪行为是,当我在不同类型的两个选项卡之间切换时,会创建用户控件的一个新实例.当我在相同类型的两个选项卡之间切换时,WPF 重用用户控件的相同实例,它只是更改控件上的 DataContext.

The strange behavior I observe is, when I change between two tabs of a different type, a new instance of the user control is created. When I change between two tabs of the same type, WPF reuses the same instance of the user control, it just changes the DataContext on the control.

这会带来一些非常不幸的后果,例如在两个相同类型但视觉状态不同的选项卡之间切换时,则显示视觉状态转换动画,而该位置不应该显示;变化应该是即时的.在两个不同类型的选项卡之间切换时也会发生一些异常.

This has some very unfortunate consequences, e.g. when changing between two tabs of the same type, but with different visual state, then the visual state transition animation is displayed, where it should not; the change should be instant. Also some exceptions occur when changing between two tabs of different type.

我是否可以更改此行为,以便在切换到不同的选项卡时,选项卡控件将为每个选项卡页保留一个用户控件实例,而不是破坏"控件.

Can I change this behavior so the tab control will hold one instance of the user control for each tab page, and not "destroy" the controls, when switching to a different tab.

<UserControl.Resources>
    <DataTemplate DataType="{x:Type ViewModels:ConcreteViewModel1}" d:IsDataSource="true">
        <Views:ConcreteView1 />
    </DataTemplate>
    <DataTemplate DataType="{x:Type ViewModels:ConcreteViewModel2}" d:IsDataSource="true">
        <Views:ConcreteView2/>
    </DataTemplate>
    ...
</UserControl.Resources>

<Grid x:Name="ControlTabLayoutRoot">
    <TabControl Grid.Row="0" x:Name="Main_TabControl"
        ItemsSource="{Binding MainTabControl}"
        SelectedIndex="{Binding SelectedIndex}" 
        IsSynchronizedWithCurrentItem="True"
        HorizontalContentAlignment="Stretch" 
        VerticalContentAlignment="Stretch"
        ItemContainerStyle="{DynamicResource CustomTabItemStyle}"
        Style="{DynamicResource CustomTabControl}">
    <TabControl.ItemTemplate>
        <DataTemplate>
            <TextBlock>                 
                <TextBlock.Text>
                  <MultiBinding Converter="{StaticResource tabItemHeaderConverter}">
                      <MultiBinding.Bindings>
                          <Binding/>
                          <Binding Path="ProtocolName"/>
                      </MultiBinding.Bindings>
                  </MultiBinding>
                </TextBlock.Text>
            </TextBlock>
        </DataTemplate>
    </TabControl.ItemTemplate>
    </TabControl>
</Grid>

推荐答案

A TabControl 在内部进行虚拟化回收.您可以在此 StackOverflow 问题中找到两种不同的方法来防止这种情况:

A TabControl does recycling virtualization internally. You can find two different approaches to prevent this in this StackOverflow questions:

这篇关于为什么选项卡控件在更改选项卡时重用 View 实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆