How can I conditionally hide the TabPanel portion of a TabControl in its entirety?

牧云@^-^@ 提交于 2019-12-12 04:03:24

问题


I've found a lot of variations of this question, but the conversation always seems to revolve around the individual TabItems, and not the TabPanel as a thing unto itself.

My main window has a TabControl on it. The tabs are views. One of those views is special...a navigation display, whereas all of the others are the "sections" that the nav view can open.

What I am trying to accomplish is that when the user is viewing the nav view, the tabs all go away. I.e. hide the whole TabPanel instead of having to hide each TabItem one-by-one. While viewing any other page, the tabs show, for easy movement between views.

I created this question in response to a suggestion made on my other question.

The problem I have is that the TabPanel does not seem to have a Template that I can override in order to do something like a DataTrigger bound to the Visibility property. The closest I have gotten is a plain old Style.Setter.

Any suggestions on how to get what I am after?


回答1:


You basically provided the answer yourself - the right combination is to use a Style together with a DataTrigger. The trick is to define a Style with TargetType set to {x:Type TabPanel}, and put it as a resource of the TabControl - that way the style will be applied to the TabPanel (because it's an implicit style). Here's an example:

<TabControl (...)>
    <TabControl.Resources>
        <Style TargetType="{x:Type TabPanel}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding SelectedIndex, RelativeSource={RelativeSource AncestorType=TabControl}}"
                             Value="0">
                    <Setter Property="Visibility" Value="Collapsed" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </TabControl.Resources>
    (...)
</TabControl>

In this example the panel will be collapsed while the first item is selected.



来源:https://stackoverflow.com/questions/45311882/how-can-i-conditionally-hide-the-tabpanel-portion-of-a-tabcontrol-in-its-entiret

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!