如何$标签项目内p $ pserve控制状态在一个TabControl [英] How to preserve control state within tab items in a TabControl

查看:156
本文介绍了如何$标签项目内p $ pserve控制状态在一个TabControl的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个初来乍到WPF,试图构建如下约什 - 史密斯的优秀文章的描述的模型 - 视图 - 视图模型设计模式

I am a newcomer to WPF, attempting to build a project that follows the recommendations of Josh Smith's excellent article describing The Model-View-ViewModel Design Pattern.

使用Josh的样本code作为一个基地,我创建了一个简单的应用程序,它包含了一些工作区,每个重由一个TabControl选项卡psented $ P $。在我的应用程序,工作区是一个文档编辑器,允许分级文件通过一个TreeView控件进行操作。

Using Josh's sample code as a base, I have created a simple application that contains a number of "workspaces", each represented by a tab in a TabControl. In my application, a workspace is a document editor that allows a hierarchical document to be manipulated via a TreeView control.

虽然我已经成功地打开多个工作区和观看在绑定TreeView控件的文档内容,我发现树视图忘记自己的状态选项卡之间切换时。例如,如果在TAB1树视图中部分膨胀,将示出为切换到TAB2以及返回TAB1后完全折叠。这种行为似乎适用于所有控件控制状态的各个方面。

Although I have succeeded in opening multiple workspaces and viewing their document content in the bound TreeView control, I find that the TreeView "forgets" its state when switching between tabs. For example, if the TreeView in Tab1 is partially expanded, it will be shown as fully collapsed after switching to Tab2 and returning to Tab1. This behaviour appears to apply to all aspects of control state for all controls.

一些实验后,我意识到,我可以通过每个控制国家财产明确绑定到底层视图模型上的专用属性TabItem的内preserve状态。然而,这似乎是很多额外的工作,当我只希望我的所有控件在工作区之间切换时,记住它们的状态。

After some experimentation, I have realized that I can preserve state within a TabItem by explicitly binding each control state property to a dedicated property on the underlying ViewModel. However, this seems like a lot of additional work, when I simply want all my controls to remember their state when switching between workspaces.

我想我失去了一些东西简单,但我不知道到哪里寻找答案。任何指导将是非常美联社preciated。

I assume I am missing something simple, but I am not sure where to look for the answer. Any guidance would be much appreciated.

谢谢,
蒂姆·

Thanks, Tim

更新:

按照要求,我会尝试张贴一些code演示了这个问题。然而,由于构成了这一TreeView的是复杂的数据,我将发布一个简化的例子,显示出相同的symtoms。下面是从主窗口中的XAML:

As requested, I will attempt to post some code that demonstrates this problem. However, since the data that underlies the TreeView is complex, I will post a simplified example that exhibits the same symtoms. Here is the XAML from the main window:

<TabControl IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding Path=Docs}">
    <TabControl.ItemTemplate>
        <DataTemplate>
            <ContentPresenter Content="{Binding Path=Name}" />
        </DataTemplate>
    </TabControl.ItemTemplate>

    <TabControl.ContentTemplate>
        <DataTemplate>
            <view:DocumentView />
        </DataTemplate>
    </TabControl.ContentTemplate>
</TabControl>

以上XAML正确结合​​DocumentViewModel的一个ObservableCollection,其中每个成员都是通过DocumentView psented $ P $。

The above XAML correctly binds to an ObservableCollection of DocumentViewModel, whereby each member is presented via a DocumentView.

有关该实施例的简单性,我已经从DocumentView除去树视图(上面提到的)中,用含3个固定标签一个TabControl代替它:

For the simplicity of this example, I have removed the TreeView (mentioned above) from the DocumentView and replaced it with a TabControl containing 3 fixed tabs:

<TabControl>
    <TabItem Header="A" />
    <TabItem Header="B" />
    <TabItem Header="C" />
</TabControl>

在这种情况下,有DocumentView和DocumentViewModel之间没有约束力。当code运行时,内部TabControl的是无法记住其选择外TabControl的切换时。

In this scenario, there is no binding between the DocumentView and the DocumentViewModel. When the code is run, the inner TabControl is unable to remember its selection when the outer TabControl is switched.

不过,如果我明确地内的TabControl的selectedIndex属性绑定...

However, if I explicitly bind the inner TabControl's SelectedIndex property ...

<TabControl SelectedIndex="{Binding Path=SelectedDocumentIndex}">
    <TabItem Header="A" />
    <TabItem Header="B" />
    <TabItem Header="C" />
</TabControl>

...要在DocumentViewModel相应的虚拟财产...

... to a corresponding dummy property on the DocumentViewModel ...

public int SelecteDocumentIndex { get; set; }

...内标签能够记住它的选择。

... the inner tab is able to remember its selection.

我知道我可以有效地应用这种技术来控制每一个每一个视觉特性解决我的问题,但我希望有一个更优雅的解决方案。

I understand that I can effectively solve my problem by applying this technique to every visual property of every control, but I am hoping there is a more elegant solution.

推荐答案

的作家示例应用程序WPF应用程序框架(WAF) 展示了如何解决您的问题。它为每一个TabItem的一个新的用户控件。所以状态是preserved当用户更改活动选项卡。

The Writer sample application of the WPF Application Framework (WAF) shows how to solve your issue. It creates a new UserControl for every TabItem. So the state is preserved when the user changes the active Tab.

这篇关于如何$标签项目内p $ pserve控制状态在一个TabControl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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