繁体   English   中英

WPF TabControl模板+ ItemContainerStyle

[英]WPF TabControl Templates + ItemContainerStyle

我试图创建一个标签控件样式,该样式基本上看起来像是顶部居中的按钮,以及下面显示内容的内容面板。

我对模板有点陌生,但是到目前为止,除了一件事以外,我的工作效果很好。 我希望能够为文本元素设置默认的背景色。 通常,我通过将ContentPresenter与依赖项元素结合使用来完成此任务。 像这样

<ContentPresenter TextElement.Foreground="White"/>

这基本上使此Presenter编写的任何TextElement控件都可以继承此属性。

现在,我正在尝试做同样的事情,但是没有用! 我认为这与我的风格错误有关。

样式:

<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 =“ White”属性,但不会打印白色文本!

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

<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>

我知道这很复杂,但是我真的很喜欢这样做。

找到解决方案。

根据HCL的帖子,我找到了一个解决方案。 我正遇到同样的问题,我试图让内容演示者设置继承的依赖属性。 相反,我只是简单地告诉模板应用dependency属性,那样,每个tabitem都将样式设置为具有此属性,从而为其所有子项设置它。

<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>

我真正不做的只是添加以下行:

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

之前的控制模板! 我也将白色文本从内容演示者中删除,因为它没有用。

查看此帖子,它对我来说具有相同的效果:

ControlTemplate中的ContentPresenter无法更改附加的依赖项属性

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM