为什么标签控件改变标签时重用查看实例 [英] Why do tab controls reuse View instances when changing tab

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

问题描述

我有以下的MVVM模式的WPF项目。

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

实施标签控件XAML文件使用一个数据模板选择特定的用户控件来显示,这取决于具体类型观察的集合视图模式。

奇怪的行为我观察的是,当我一个不同类型的两个选项卡之间切换,创建用户控制的新实例。当我同类型的两个选项卡之间切换,WPF重用用户控件的同一个实例,它只是改变了在控制DataContext的。

这有一些非常不幸的后果,例如,同一类型的两个选项卡,但具有不同的视觉状态之间切换时,则显示的可视状态过渡动画,它不应该;改变应该是瞬间。不同类型的两个选项卡之间切换时也可能出现一些例外。

我可以改变这种行为,以便选项卡控件将保存各个标签页的用户控件的一个实例,而不是消灭的控制,切换到不同的选项卡时。

 < UserControl.Resources>
    <数据类型的DataTemplate ={X:类型的ViewModels:ConcreteViewModel1}D:IsDataSource =真正的>
        <点击:ConcreteView1 />
    < / DataTemplate中>
    <数据类型的DataTemplate ={X:类型的ViewModels:ConcreteViewModel2}D:IsDataSource =真正的>
        <点击:ConcreteView2 />
    < / DataTemplate中>
    ...
< /UserControl.Resources><电网X:NAME =ControlTabLayoutRoot>
    &所述;的TabControl Grid.Row =0×:名称=Main_TabControl
        的ItemsSource ={结合MainTabControl}
        的SelectedIndex ={结合的SelectedIndex}
        IsSynchronizedWithCurrentItem =真
        Horizo​​ntalContentAlignment =弹力
        VerticalContentAlignment =弹力
        ItemContainerStyle ={DynamicResource CustomTabItemStyle}
        风格={DynamicResource CustomTabControl}>
    < TabControl.ItemTemplate>
        <&DataTemplate的GT;
            <&的TextBlock GT;
                < TextBlock.Text>
                  < MultiBinding转换器={StaticResource的tabItemHeaderConverter}>
                      < MultiBinding.Bindings>
                          <装订/>
                          <绑定路径=ProtocolName/>
                      < /MultiBinding.Bindings>
                  < / MultiBinding>
                < /TextBlock.Text>
            < / TextBlock的>
        < / DataTemplate中>
    < /TabControl.ItemTemplate>
    < / TabControl的>
< /网格和GT;


解决方案

A 的TabControl 确实在内部循环利用虚拟化。你可以找到在这个计算器问题两种不同的方法prevent这样的:

I have a WPF project following an MVVM pattern.

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.

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.

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 does recycling virtualization internally. You can find two different approaches to prevent this in this StackOverflow questions:

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

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