首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WPF TabControl模板+ ItemContainerStyle

WPF TabControl模板+ ItemContainerStyle
EN

Stack Overflow用户
提问于 2010-08-13 04:25:58
回答 2查看 11.1K关注 0票数 2

我正在尝试创建一个Tab控件样式,它基本上看起来像顶部居中的按钮和显示tabitem内容的内容面板。

我对模板有点陌生,但到目前为止,除了一件事之外,我的工作都很好。我希望能够设置文本元素的默认forground颜色。通常,我通过使用带有依赖项元素的ContentPresenter来实现这一点。所以就像这样。

代码语言:javascript
复制
<ContentPresenter TextElement.Foreground="White"/>

这基本上使此演示者编写的任何TextElement控件都继承此属性。

现在我正试着做同样的事情,但它不起作用!我相信这与我的风格错误有关。

风格:

代码语言:javascript
复制
<Style x:Key="MainMenuTab" TargetType="{x:Type TabControl}">



        <Setter Property="Template">
            <Setter.Value>

                <ControlTemplate TargetType="{x:Type TabControl}">

                    <Grid KeyboardNavigation.TabNavigation="Local" Width="{TemplateBinding Width}">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="*"/>
                        </Grid.RowDefinitions>

                        <!-- Tab Headers Panel -->
                        <Grid Grid.Row="0" Background="{StaticResource Brush_ApplicationTabBackground}">

                            <TabPanel 
                                Name="HeaderPanel"
                                Grid.Row="0"
                                Panel.ZIndex="1" 
                                HorizontalAlignment="Center"
                                VerticalAlignment="Center"
                                IsItemsHost="True"
                                KeyboardNavigation.TabIndex="1"
                                Background="{StaticResource Brush_ApplicationTabBackground}"
                                >


                            </TabPanel>


                        </Grid>



                        <!-- Tab Body  -->
                        <Border 
                            Name="Border" 
                            Grid.Row="1" 
                            Background="{StaticResource Brush_ApplicationBackground}"
                            BorderBrush="Transparent"
                            BorderThickness="1" 
                            CornerRadius="2" 
                            KeyboardNavigation.TabNavigation="Local"
                            KeyboardNavigation.DirectionalNavigation="Contained"
                            KeyboardNavigation.TabIndex="2" >

                            <ContentPresenter 
                                Name="PART_SelectedContentHost"
                                Margin="4"
                                ContentSource="SelectedContent" />
                        </Border>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>




        <!-- Each Tab should look like this -->
        <Setter Property="ItemContainerStyle">
            <Setter.Value>
                <Style>

                    <Setter Property="Control.Template">
                        <Setter.Value>

                            <ControlTemplate TargetType="{x:Type TabItem}">

                                <Grid Background="{StaticResource Brush_ApplicationTabBackground}">
                                    <Border Width="50" x:Name="BorderTab" Height="50" Margin="5" BorderThickness="1" ClipToBounds="True" BorderBrush="Transparent" Background="Transparent" CornerRadius="5">
                                        <Rectangle x:Name="BackgroundRec" Fill="Transparent" Stroke="Transparent" Width="50" Height="50" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                                    </Border>
                                    <ContentPresenter Name="TheHeaderContentPresenter" Width="50" Height="50" Margin="5" ContentSource="Header" HorizontalAlignment="Center" VerticalAlignment="Center" TextElement.Foreground="White"/>
                                </Grid>

                                <ControlTemplate.Triggers>
                                    <Trigger Property="IsSelected" Value="True">
                                        <Trigger.Setters>
                                            <Setter TargetName="BorderTab" Property="BorderBrush" Value="{StaticResource Brush_ApplicationTabHighlight}"/>
                                            <Setter TargetName="BorderTab" Property="BorderThickness" Value="3"/>
                                            <Setter TargetName="BackgroundRec" Property="Fill" Value="{StaticResource Brush_ApplicationTabHighlight}"/>
                                            <Setter Property="Panel.ZIndex" Value="1"/>
                                        </Trigger.Setters>
                                    </Trigger>

                                    <Trigger Property="IsSelected" Value="False">
                                        <Trigger.Setters>
                                            <Setter TargetName="BorderTab" Property="BorderBrush" Value="{StaticResource Brush_ApplicationTabBackground}"/>
                                            <Setter TargetName="BorderTab" Property="BorderThickness" Value="0"/>
                                            <Setter TargetName="BackgroundRec" Property="Fill" Value="{StaticResource Brush_ApplicationTabBackground}"/>
                                        </Trigger.Setters>
                                    </Trigger>
                                </ControlTemplate.Triggers>

                            </ControlTemplate>

                        </Setter.Value>
                    </Setter>

                </Style>
            </Setter.Value>
        </Setter>

在我的ContentPresenter中,ItemContainerStyle下的TextElement.Foreground=有“白色”属性,但它不会打印白色文本!

我使用此样式的tabcontrol如下所示:

代码语言:javascript
复制
<TabControl Grid.Row="2" Style="{StaticResource MainMenuTab}">


            <TabItem>
                <TabItem.Header>
                    <TextBlock VerticalAlignment="Bottom" HorizontalAlignment="Center" Margin="0,0,0,5" Text="{x:Static  UIStrings:ClientStrings.MainWindow_TabHeader_SingleWaveLength}"></TextBlock>
                </TabItem.Header>
                <TextBlock>TEST PANEL</TextBlock>


       </TabItem>
</TabControl>

我知道这很复杂,但我真的希望它能工作。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2010-08-13 05:06:26

找到解决方案。

基于HCL的帖子,我找到了一个解决方案。我遇到了同样的问题,我正试图让内容呈现者设置继承的依赖属性。相反,我只是简单地告诉模板应用dependance属性,这样每个tabitem的样式就会具有该属性,从而为它的所有子项设置该属性。

代码语言:javascript
复制
<Setter Property="ItemContainerStyle">
    <Setter.Value>
        <Style TargetType="TabItem">

            <Setter Property="TextElement.Foreground" Value="White"/>

            <Setter Property="Template">
                <Setter.Value>

                    <ControlTemplate TargetType="{x:Type TabItem}">

                        <Grid Background="{StaticResource Brush_ApplicationTabBackground}">
                            <Border Width="50" x:Name="BorderTab" Height="50" Margin="5" BorderThickness="1" ClipToBounds="True" BorderBrush="Transparent" Background="Transparent" CornerRadius="5">
                                <Rectangle x:Name="BackgroundRec" Fill="Transparent" Stroke="Transparent" Width="50" Height="50" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                            </Border>
                            <ContentPresenter Name="TheHeaderContentPresenter" Width="50" Height="50" Margin="5" ContentSource="Header" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                        </Grid>

                        <ControlTemplate.Triggers>
                            <Trigger Property="IsSelected" Value="True">
                                <Trigger.Setters>
                                    <Setter TargetName="BorderTab" Property="BorderBrush" Value="{StaticResource Brush_ApplicationTabHighlight}"/>
                                    <Setter TargetName="BorderTab" Property="BorderThickness" Value="3"/>
                                    <Setter TargetName="BackgroundRec" Property="Fill" Value="{StaticResource Brush_ApplicationTabHighlight}"/>
                                    <Setter Property="Panel.ZIndex" Value="1"/>
                                </Trigger.Setters>
                            </Trigger>

                            <Trigger Property="IsSelected" Value="False">
                                <Trigger.Setters>
                                    <Setter TargetName="BorderTab" Property="BorderBrush" Value="{StaticResource Brush_ApplicationTabBackground}"/>
                                    <Setter TargetName="BorderTab" Property="BorderThickness" Value="0"/>
                                    <Setter TargetName="BackgroundRec" Property="Fill" Value="{StaticResource Brush_ApplicationTabBackground}"/>
                                </Trigger.Setters>
                            </Trigger>
                        </ControlTemplate.Triggers>

                    </ControlTemplate>

                </Setter.Value>
            </Setter>

        </Style>
    </Setter.Value>
</Setter>

我真正没有做的就是添加了下面这行:

代码语言:javascript
复制
<Setter Property="TextElement.Foreground" Value="White"/>

在控件模板之前!此外,我从内容呈现器中删除了白色文本,因为它是无用的。

票数 3
EN

Stack Overflow用户

发布于 2010-08-13 04:35:43

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3471738

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档