在TabItem上触发IsMouseOver

时间:2013-10-28 20:10:50

标签: wpf xaml

我的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,但它引入了另一个问题。仅当光标位于tabitem的标题上时才会触发IsMouseOver事件。另一个用户对here描述了同样的问题。建议的答案是将Background =“Transparent”值添加到Border,但它对我没用。

1 个答案:

答案 0 :(得分:2)

如果我理解您的问题,您只需将SourceName上的Trigger更改为ContentSite的边框。

请尝试以下风格:

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