WPF Styles for TabControl / TabPanel / TabItem

后端 未结 2 1467
悲&欢浪女
悲&欢浪女 2021-02-09 00:17

Here\'s a newbie question on the WPF TabControl, TabItem and TabPanel. There is a related question on StackOVF with an answer I happily used in my app. Here\'s a link to the ans

相关标签:
2条回答
  • 2021-02-09 00:49

    You'll probably need to create a ControlTemplate to do this.

    I'm not very familiar with ControlTemplates yet. I hacked this example from: http://msdn.microsoft.com/en-us/library/system.windows.controls.primitives.tabpanel.aspx

    <Style  TargetType="{x:Type TabControl}">
            <Setter Property="OverridesDefaultStyle" Value="True" />
            <Setter Property="SnapsToDevicePixels" Value="True" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type TabControl}">
                        <Grid KeyboardNavigation.TabNavigation="Local">
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto"/>
                                <RowDefinition Height="*"/>
                            </Grid.RowDefinitions>
                            <TabPanel Name="HeaderPanel"
                                      Grid.Row="0"
                                      Panel.ZIndex="1" 
                                      Margin="0,0,4,-1" 
                                      IsItemsHost="True"
                                      KeyboardNavigation.TabIndex="1"
                                      HorizontalAlignment="Center"/>
                          </Grid>
    
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    
    0 讨论(0)
  • 2021-02-09 00:51

    alt text

    TabControl uses a specialized TabPanel class and not a generic Panel like StackPanel because if you mess around with the TabControl you'll realize that the panel does quite a few things which generic panels dont. One is adjusting the tab header items in multiple rows. Another is that the the rows of items will be rearranged so that the selected tabitem header is always in the last row. I guess it might be doing even more

    I am quite interested in knowing why putting the style in the window resource section does not work. My initial reaction was it should work until I tried it. I am adding this as an answer because SO wont let me add an image in a comment.

    0 讨论(0)
提交回复
热议问题