删除WPF中ListView上的鼠标悬停效果 [英] Remove the mouse over effect on a ListView in WPF

查看:92
本文介绍了删除WPF中ListView上的鼠标悬停效果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎样才能使淡蓝色的鼠标对我的ListView产生影响?

How can I get ride of the pale blue mouse over effect on my ListView?

当我触摸屏幕时,当我上下滚动时,一个淡蓝色的选择器出现并停留在屏幕中间(但是以深蓝色突出显示的所选项目不会改变).我猜这是鼠标悬停效果,因为使用鼠标时也会出现同样的效果.

When I touch the screen a pale blue selector appears and stays in the middle of the screen as I scroll up and down (but the selected item which is highlighted in a darker blue doesn't change). I'm guessing it's the mouse over effect as the same happens effect appears when I use the mouse.

如何解决?

我将DataTemplate用于项目集合.

I use a DataTemplate for the items collection.

代码

<ListView Grid.Row="1"
              Margin="10"                  
              HorizontalContentAlignment="Stretch"
              ItemsSource="{Binding Source={StaticResource MyData}}"
              ItemTemplate="{StaticResource MyItemTemplate}"
              ScrollViewer.CanContentScroll="False"
              ScrollViewer.PanningMode="VerticalOnly"
              ScrollViewer.PanningRatio="0.5">
    </ListView>

这是我的物品模板:

<DataTemplate x:Key="MyItemTemplate">
        <Grid Margin="10,5">                
            <Grid.RowDefinitions>
                <RowDefinition />
                <RowDefinition />
            </Grid.RowDefinitions>
            <Border BorderBrush="Gray"
                    BorderThickness="1"
                    Grid.RowSpan="2"
                    CornerRadius="5" />                
            <TextBlock Text="{Binding Name}"
                       FontSize="20"
                       VerticalAlignment="Center"
                       Grid.Row="0"
                       Margin="10" />
            <Border Background="#FFB9B9B9"
                    Grid.Row="1"
                    CornerRadius="5"
                    Margin="10,0,10,4">
            <StackPanel HorizontalAlignment="Stretch"                            
                        Orientation="Horizontal"                            
                        Grid.Row="1">                    
                <TextBlock VerticalAlignment="Center"
                           Text="Status: "
                           Margin="5,5,0,5" />
                <TextBlock VerticalAlignment="Center"
                           Text="{Binding CompletionStatus}" />
                <TextBlock VerticalAlignment="Center"
                           Text="% complete, " />
                <TextBlock VerticalAlignment="Center"
                           Text="Upload status: " />
                <TextBlock VerticalAlignment="Center"
                           Text="{Binding UploadStatus}" />
                <TextBlock VerticalAlignment="Center"
                           Text="last Modified: " />
                <TextBlock VerticalAlignment="Center"
                           Text="{Binding LastModified}" />
            </StackPanel>
            </Border>
        </Grid>
    </DataTemplate>        

推荐答案

我可以使它起作用的唯一方法是重新定义ListViewItem ControlTemplate.请尝试下面的代码,看看它是否可以解决您的问题:

The only way I could get this to work was to redefine the ListViewItem ControlTemplate. Give the code below a try and see if it resolves your issue:

ListViewItem Style:

ListViewItem Style:

<Style x:Key="LvItemStyle" TargetType="ListViewItem">
<Setter Property="Template">
    <Setter.Value>
        <ControlTemplate TargetType="ListViewItem">
            <Border x:Name="border" Background="Transparent">
                <VisualStateManager.VisualStateGroups>
                    <VisualStateGroup x:Name="CommonStates">
                        <VisualState x:Name="Normal" />
                        <VisualState x:Name="Disabled" />
                    </VisualStateGroup>
                    <VisualStateGroup x:Name="SelectionStates">
                        <VisualState x:Name="Unselected" />
                        <VisualState x:Name="Selected">
                            <Storyboard>
                                <ColorAnimationUsingKeyFrames Storyboard.TargetName="border"
                                                              Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)">
                                    <EasingColorKeyFrame KeyTime="0" Value="LightBlue" />
                                </ColorAnimationUsingKeyFrames>
                            </Storyboard>
                        </VisualState>
                        <VisualState x:Name="SelectedUnfocused">
                            <Storyboard>
                                <ColorAnimationUsingKeyFrames Storyboard.TargetName="border"
                                                              Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)">
                                    <EasingColorKeyFrame KeyTime="0" Value="SkyBlue" />
                                </ColorAnimationUsingKeyFrames>
                            </Storyboard>
                        </VisualState>
                    </VisualStateGroup>
                </VisualStateManager.VisualStateGroups>
                <ContentPresenter/>
            </Border>
        </ControlTemplate>
    </Setter.Value>
</Setter>

ListView:

ListView:

    <Grid Background="DarkGray">
    <ListView Grid.Row="1"
          Margin="10"                  
          HorizontalContentAlignment="Stretch"
          ItemsSource="{Binding MyItems}"
          ItemTemplate="{StaticResource LvDataTemplate}"
          ItemContainerStyle="{StaticResource LvItemStyle}"
          ScrollViewer.CanContentScroll="False"
          ScrollViewer.PanningMode="VerticalOnly"
          ScrollViewer.PanningRatio="0.5">
    </ListView>
</Grid>

出于演示目的,我已经为Selected VisualStates的颜色进行了硬编码.理想情况下,您将从资源文件中获取这些信息.

I have hardcoded the colors for the Selected VisualStates for demonstration purposes. Ideally you would get these from a resource file.

这篇关于删除WPF中ListView上的鼠标悬停效果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆