关闭ListView边框鼠标悬停动画

时间:2012-04-19 15:07:38

标签: c# wpf listview styles controltemplate

将鼠标悬停在控件上时,

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>

这会产生一个绿色边框,当你褪色为淡蓝色时,鼠标悬停时会短暂变红。默认动画优先。

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

1 个答案:

答案 0 :(得分:1)

您必须覆盖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>

如果您想要删除焦点外观,那么最好只用ListBoxChrome元素替换Border