抑制GridViewColumn上的鼠标悬停效果

时间:2011-02-28 22:03:03

标签: c# wpf xaml datatemplate

我有一个GridViewColumnHeaderTemplateImageTextBlock。当用户将鼠标悬停在Image上时,我正在更改其不透明度,但我仍然会获得默认的标题鼠标效果。当用户将鼠标悬停在图像上时,如何抑制此效果?

1 个答案:

答案 0 :(得分:4)

IsMouseOver触发器位于GridViewColumnHeader模板内。看起来像这样

<Trigger Property="IsMouseOver" Value="true">
    <Setter Property="Background" TargetName="HeaderBorder" Value="{StaticResource GridViewColumnHeaderHoverBackground}"/>
    <Setter Property="BorderBrush" TargetName="HeaderHoverBorder" Value="#FF88CBEB"/>
    <Setter Property="Visibility" TargetName="UpperHighlight" Value="Visible"/>
    <Setter Property="Background" TargetName="PART_HeaderGripper" Value="Transparent"/>
</Trigger>

更新

如果有一些代码,您可以在GridViewColumnHeader的已加载事件中禁用IsMouseOver触发器。它通过设置值来覆盖触发器中的setter,这也适用于经典主题。

<ListView ...>
    <ListView.Resources>
        <Style TargetType="{x:Type GridViewColumnHeader}">
            <EventSetter Event="Loaded" Handler="GridViewColumnHeader_Loaded"/>
        </Style>
    </ListView.Resources>
    <!--...-->
</ListView>

事件处理程序背后的代码

private void GridViewColumnHeader_Loaded(object sender, RoutedEventArgs e)
{
    GridViewColumnHeader columnHeader = sender as GridViewColumnHeader;
    Border HeaderBorder = columnHeader.Template.FindName("HeaderBorder", columnHeader) as Border;
    if (HeaderBorder != null)
    {
        HeaderBorder.Background = HeaderBorder.Background;
    }
    Border HeaderHoverBorder = columnHeader.Template.FindName("HeaderHoverBorder", columnHeader) as Border;
    if (HeaderHoverBorder != null)
    {
        HeaderHoverBorder.BorderBrush = HeaderHoverBorder.BorderBrush;
    }
    Rectangle UpperHighlight = columnHeader.Template.FindName("UpperHighlight", columnHeader) as Rectangle;
    if (UpperHighlight != null)
    {
        UpperHighlight.Visibility = UpperHighlight.Visibility;
    }
    Thumb PART_HeaderGripper = columnHeader.Template.FindName("PART_HeaderGripper", columnHeader) as Thumb;
    if (PART_HeaderGripper != null)
    {
        PART_HeaderGripper.Background = PART_HeaderGripper.Background;
    }
}

Aero解决方案,删除了IsMouseOver触发器的默认样式

<LinearGradientBrush x:Key="GridViewColumnHeaderBackground" EndPoint="0,1" StartPoint="0,0">
    <GradientStop Color="#FFFFFFFF" Offset="0"/>
    <GradientStop Color="#FFFFFFFF" Offset="0.4091"/>
    <GradientStop Color="#FFF7F8F9" Offset="1"/>
</LinearGradientBrush>
<LinearGradientBrush x:Key="GridViewColumnHeaderBorderBackground" EndPoint="0,1" StartPoint="0,0">
    <GradientStop Color="#FFF2F2F2" Offset="0"/>
    <GradientStop Color="#FFD5D5D5" Offset="1"/>
</LinearGradientBrush>
<LinearGradientBrush x:Key="GridViewColumnHeaderHoverBackground" EndPoint="0,1" StartPoint="0,0">
    <GradientStop Color="#FFBDEDFF" Offset="0"/>
    <GradientStop Color="#FFB7E7FB" Offset="1"/>
</LinearGradientBrush>
<LinearGradientBrush x:Key="GridViewColumnHeaderPressBackground" EndPoint="0,1" StartPoint="0,0">
    <GradientStop Color="#FF8DD6F7" Offset="0"/>
    <GradientStop Color="#FF8AD1F5" Offset="1"/>
</LinearGradientBrush>
<Style x:Key="GridViewColumnHeaderGripper" TargetType="{x:Type Thumb}">
    <Setter Property="Canvas.Right" Value="-9"/>
    <Setter Property="Width" Value="18"/>
    <Setter Property="Height" Value="{Binding ActualHeight, RelativeSource={RelativeSource TemplatedParent}}"/>
    <Setter Property="Padding" Value="0"/>
    <Setter Property="Background" Value="{StaticResource GridViewColumnHeaderBorderBackground}"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Thumb}">
                <Border Background="Transparent" Padding="{TemplateBinding Padding}">
                    <Rectangle Fill="{TemplateBinding Background}" HorizontalAlignment="Center" Width="1"/>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
<Style TargetType="{x:Type GridViewColumnHeader}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type GridViewColumnHeader}">
                <Grid SnapsToDevicePixels="true">
                    <Border x:Name="HeaderBorder" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="0,1,0,1" Background="{TemplateBinding Background}">
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition MaxHeight="7"/>
                                <RowDefinition/>
                            </Grid.RowDefinitions>
                            <Rectangle x:Name="UpperHighlight" Fill="#FFE3F7FF" Visibility="Collapsed"/>
                            <Border Padding="{TemplateBinding Padding}" Grid.RowSpan="2">
                                <ContentPresenter x:Name="HeaderContent" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="0,0,0,1" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                            </Border>
                        </Grid>
                    </Border>
                    <Border x:Name="HeaderHoverBorder" BorderThickness="1,0,1,1" Margin="1,1,0,0"/>
                    <Border x:Name="HeaderPressBorder" BorderThickness="1,1,1,0" Margin="1,0,0,1"/>
                    <Canvas>
                        <Thumb x:Name="PART_HeaderGripper" Style="{StaticResource GridViewColumnHeaderGripper}"/>
                    </Canvas>
                </Grid>
                <ControlTemplate.Triggers>
                    <!--<Trigger Property="IsMouseOver" Value="true">
                        <Setter Property="Background" TargetName="HeaderBorder" Value="{StaticResource GridViewColumnHeaderHoverBackground}"/>
                        <Setter Property="BorderBrush" TargetName="HeaderHoverBorder" Value="#FF88CBEB"/>
                        <Setter Property="Visibility" TargetName="UpperHighlight" Value="Visible"/>
                        <Setter Property="Background" TargetName="PART_HeaderGripper" Value="Transparent"/>
                    </Trigger>-->
                    <Trigger Property="IsPressed" Value="true">
                        <Setter Property="Background" TargetName="HeaderBorder" Value="{StaticResource GridViewColumnHeaderPressBackground}"/>
                        <Setter Property="BorderBrush" TargetName="HeaderHoverBorder" Value="#FF95DAF9"/>
                        <Setter Property="BorderBrush" TargetName="HeaderPressBorder" Value="#FF7A9EB1"/>
                        <Setter Property="Visibility" TargetName="UpperHighlight" Value="Visible"/>
                        <Setter Property="Fill" TargetName="UpperHighlight" Value="#FFBCE4F9"/>
                        <Setter Property="Visibility" TargetName="PART_HeaderGripper" Value="Hidden"/>
                        <Setter Property="Margin" TargetName="HeaderContent" Value="1,1,0,0"/>
                    </Trigger>
                    <Trigger Property="Height" Value="Auto">
                        <Setter Property="MinHeight" Value="20"/>
                    </Trigger>
                    <Trigger Property="IsEnabled" Value="false">
                        <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Style.Triggers>
        <Trigger Property="Role" Value="Floating">
            <Setter Property="Opacity" Value="0.4082"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type GridViewColumnHeader}">
                        <Canvas x:Name="PART_FloatingHeaderCanvas">
                            <Rectangle Fill="#FF000000" Height="{TemplateBinding ActualHeight}" Opacity="0.4697" Width="{TemplateBinding ActualWidth}"/>
                        </Canvas>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Trigger>
        <Trigger Property="Role" Value="Padding">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type GridViewColumnHeader}">
                        <Border x:Name="HeaderBorder" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="0,1,0,1" Background="{TemplateBinding Background}"/>
                        <ControlTemplate.Triggers>
                            <Trigger Property="Height" Value="Auto">
                                <Setter Property="MinHeight" Value="20"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Trigger>
    </Style.Triggers>
</Style>