简体   繁体   English

目标名称的WPF样式触发器无法正常工作

[英]WPF Style Trigger with targetname not working as expected

I have an issue setting the Foreground color in a ControlTemplate with triggers. 我在使用触发器设置ControlTemplate中设置前景颜色时遇到问题。

            <LinearGradientBrush x:Key="TabItemDefaultBackgroundBrush" StartPoint="0,0" EndPoint="0,1">
                <GradientBrush.GradientStops>
                    <GradientStopCollection>
                        <GradientStop Offset="0.0" Color="#FFF" />
                        <GradientStop Offset="1.0" Color="#EEE" />
                    </GradientStopCollection>
                </GradientBrush.GradientStops>
            </LinearGradientBrush>

            <SolidColorBrush x:Key="SolidBorderBrush" Color="#888" />
            <SolidColorBrush x:Key="TabItemSelectBackgroundBrush" Color="#69C" />
            <SolidColorBrush x:Key="PressedBrush" Color="#79C" />
            <Style TargetType="{x:Type TabItem}">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type TabItem}">
                            <Grid>
                                <Border
                                    Name="tabBorder"
                                    MinWidth="150"
                                    MinHeight="50"
                                    Margin="0,0,20,0"
                                    Background="{StaticResource TabItemDefaultBackgroundBrush}"
                                    BorderBrush="{StaticResource SolidBorderBrush}"
                                    BorderThickness="1"
                                    CornerRadius="1,1,1,1">
                                    <Grid>
                                        <Grid >
                                            <TextBlock Name="HeaderHeader">
                                                </TextBlock>
                                        </Grid>
                                        <ContentPresenter
                                            x:Name="ContentSite"
                                            Margin="12,2,12,2"
                                            HorizontalAlignment="Center"
                                            VerticalAlignment="Center"
                                            ContentSource="Header"
                                            RecognizesAccessKey="True" />
                                    </Grid>
                                </Border>
                            </Grid>
                            <ControlTemplate.Triggers>
                                <Trigger Property="IsSelected" Value="True">
                                    <Setter TargetName="HeaderHeader" Property="Foreground" Value="White" />
                                    <Setter TargetName="tabBorder" Property="Background" Value="{StaticResource TabItemSelectBackgroundBrush}" />
                                </Trigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>

When the tab is selected, the background border 'tabBorder' is set correctly, but the foreground of textblock 'HeaderHeader' does not respond. 选择选项卡后,将正确设置背景边框“ tabBorder”,但文本块“ HeaderHeader”的前景未响应。

<ControlTemplate.Triggers>
    <Trigger Property="IsSelected" Value="True">
        <Setter TargetName="HeaderHeader" Property="Foreground" Value="White" />
        <Setter TargetName="tabBorder" Property="Background" Value="{StaticResource TabItemSelectBackgroundBrush}" />
    </Trigger>
</ControlTemplate.Triggers>

When I remove the TargetName and set the foreground white (to all components), it does work. 当我删除TargetName并将前景设置为白色(对所有组件)时,它确实起作用。 (but obviously also the foreground color of everything in the content pre (但显然内容pre中所有内容的前景色

<ControlTemplate.Triggers>
    <Trigger Property="IsSelected" Value="True">
        <Setter Property="Foreground" Value="White" />
        <Setter TargetName="tabBorder" Property="Background" Value="{StaticResource TabItemSelectBackgroundBrush}" />
    </Trigger>
</ControlTemplate.Triggers>

Even stranger, when I put the TargetName back in place, but change the setter property from foreground to background, it DOES work! 甚至更陌生,当我将TargetName放回原位,但是将setter属性从前景更改为背景时,它确实起作用了! Then the background is set to white for the selected tab. 然后,将所选标签的背景设置为白色。

<ControlTemplate.Triggers>
    <Trigger Property="IsSelected" Value="True">
        <Setter TargetName="HeaderHeader" Property="Background" Value="White" />
        <Setter TargetName="tabBorder" Property="Background" Value="{StaticResource TabItemSelectBackgroundBrush}" />
    </Trigger>
</ControlTemplate.Triggers>

Why on earth is it not working for what I need (=set foreground for selected tab)?! 到底为什么它不能满足我的需要(为所选标签设置前景)?

Aha, I learned something today... I was looking in the wrong direction ! 啊哈,我今天学到了一些东西……我看错了方向! After googling more on "ControlTemplate" something finally sunk in. The ContentPresenter does not show the information of the tab, it shows the label of the tab and nothing more. 在“ ControlTemplate”上进行更多搜索之后,终于有一些东西陷入ContentPresenter不显示选项卡的信息,而是显示选项卡的标签,仅此而已。 So my trigger was working just fine, but it was setting the foreground of a textblock which had no meaning at all! 所以我的触发器工作得很好,但是它设置了一个毫无意义的文本块的前台!

When I change the trigger to which control actually shows the label, it was easy peasy... 当我将触发器更改为控件实际显示标签的触发器时,这很容易实现……

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

I did have to use an attached property 'TextElement' in order to be able to set properties on text elements. 为了能够在文本元素上设置属性,我确实必须使用附加的属性'TextElement'。

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

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