4

我的TabItem风格有些问题。这是样式:

<Style TargetType="{x:Type TabItem}">
    <Setter Property="Width" Value="160"/>
    <Setter Property="Background" Value="Transparent" />
    <Setter Property="Height" Value="70"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="TabItem">
                <Border Name="Border" Background="Transparent">

                    <ContentPresenter x:Name="ContentSite"
                                    VerticalAlignment="Center"
                                    HorizontalAlignment="Left"
                                    ContentSource="Header"
                                    Margin="10,2"/>

                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsSelected" Value="True">
                        <Setter TargetName="Border" Property="Background" Value="Red" />
                    </Trigger>
                    <Trigger Property="IsSelected" Value="False">
                        <Setter TargetName="Border" Property="Background" Value="Green" />
                    </Trigger>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter TargetName="Border" Property="Background" Value="Blue" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

问题是该IsMouseOver事件也是通过在其任何子项上方移动来触发的。我已经添加SourceName="ContentSite"到触发器:

<Trigger SourceName="ContentSite" Property="IsMouseOver" Value="True">

使用此代码,IsMouseOver 不再通过移动子元素来触发,但它引入了另一个问题。IsMouseOver 事件仅在光标位于 tabitem 的标题上时触发。另一个用户在此处描述了相同的问题。建议的答案是将 Background="Transparent" 值添加到边框,但它对我不起作用。

4

1 回答 1

4

如果我正确理解您的问题,您所要做的SourceName就是TriggerContentSite.

请尝试以下样式:

<Style TargetType="{x:Type TabItem}">
    <Setter Property="Width" Value="160"/>
    <Setter Property="Background" Value="Transparent" />
    <Setter Property="Height" Value="70"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="TabItem">
                <Border Name="Border" Background="Transparent">

                    <ContentPresenter x:Name="ContentSite"
                                    VerticalAlignment="Center"
                                    HorizontalAlignment="Left"
                                    ContentSource="Header"
                                    Margin="10,2"/>

                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsSelected" Value="True">
                        <Setter TargetName="Border" Property="Background" Value="Red" />
                    </Trigger>
                    <Trigger Property="IsSelected" Value="False">
                        <Setter TargetName="Border" Property="Background" Value="Green" />
                    </Trigger>
                    <Trigger Property="IsMouseOver" Value="True" SourceName="Border">
                        <Setter TargetName="Border" Property="Background" Value="Blue" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

而且,如果您希望仅在未选择的选项卡上发生这种情况,您可以尝试以下操作MultiTrigger

<MultiTrigger>
    <MultiTrigger.Conditions>
        <Condition Property="IsMouseOver" SourceName="Border" Value="True" />
        <Condition Property="IsSelected" Value="False" />
    </MultiTrigger.Conditions>
    <Setter TargetName="Border" Property="BorderBrush" Value="Blue" />
</MultiTrigger>
于 2013-10-28T20:45:24.347 回答