C# 关闭ListView边框鼠标悬停动画

C# 关闭ListView边框鼠标悬停动画,c#,wpf,listview,styles,controltemplate,C#,Wpf,Listview,Styles,Controltemplate,ListView的默认样式将鼠标悬停在控件上时边框颜色设置为浅蓝色。有没有办法在不替换整个控件模板的情况下关闭此功能 我试过了 <ListView> <ListView.Style> <Style TargetType="ListView"> <Setter Property="BorderBrush" Value="Green"/> <Style.Triggers>

ListView
的默认样式将鼠标悬停在控件上时边框颜色设置为浅蓝色。有没有办法在不替换整个控件模板的情况下关闭此功能

我试过了

<ListView>
    <ListView.Style>
        <Style TargetType="ListView">
            <Setter Property="BorderBrush" Value="Green"/>
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">       
                    <Setter Property="BorderBrush" Value="Red" />
                </Trigger>
            </Style.Triggers>
        </Style>
    </ListView.Style>
</ListView>

这将产生一个绿色边框,在淡入淡蓝色之前,当鼠标悬停在该边框上时,该边框会短暂变为红色。默认动画优先


我是否遗漏了一些简单的内容,或者是模板覆盖的时候了?

您必须覆盖
ControlTemplate
,因为默认的模板使用一个
ListBoxChrome
元素来创建您看到的效果
ListBoxChrome
在鼠标悬停时忽略BorderBrush属性,该属性由其
RenderMouseOver
属性确定

如果需要,您仍然可以使用
ListBoxChrome
,只需删除
RenderMouseOver
属性即可。假设您使用的是
GridView
,您将使用:

xmlns:theme="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero"

...

<Style x:Key="{x:Static GridView.GridViewStyleKey}"
       TargetType="{x:Type ListView}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ListView}">
                <theme:ListBoxChrome Name="Bd"
                                     BorderThickness="{TemplateBinding BorderThickness}"
                                     BorderBrush="{TemplateBinding BorderBrush}"
                                     Background="{TemplateBinding Background}"
                                     RenderFocused="{TemplateBinding IsKeyboardFocusWithin}"
                                     SnapsToDevicePixels="true">
                    <ScrollViewer Style="{DynamicResource {x:Static GridView.GridViewScrollViewerStyleKey}}"
                                  Padding="{TemplateBinding Padding}">
                        <ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                    </ScrollViewer>
                </theme:ListBoxChrome>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsGrouping"
                             Value="true">
                        <Setter Property="ScrollViewer.CanContentScroll"
                                Value="false"/>
                    </Trigger>
                    <Trigger Property="IsEnabled"
                             Value="false">
                        <Setter TargetName="Bd"
                                Property="Background"
                                Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
xmlns:theme=“clr命名空间:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero”
...

如果您想删除焦点外观,那么最好将
ListBoxChrome
替换为
Border
元素。

太棒了,谢谢!即使使用Snoop,我也不知道动画是从哪里来的。还不太熟悉chrome的工作原理,所以你的答案非常有用。