【问题标题】:Remove the mouse over effect on a ListView in WPF删除 WPF 中 ListView 上的鼠标悬停效果
【发布时间】:2013-06-15 20:06:13
【问题描述】:

如何让我的ListView 上的淡蓝色鼠标悬停效果?

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

如何解决?


我为项目集合使用 DataTemplate。

代码

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

【问题讨论】:

    标签: wpf xaml listview mouseover touchscreen


    【解决方案1】:

    编辑:

    我可以让它工作的唯一方法是重新定义ListViewItemControlTemplate。试试下面的代码,看看它是否能解决您的问题:

    ListViewItemStyle:

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

        <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 的颜色。理想情况下,您会从资源文件中获取这些信息。

    【讨论】:

    • 嗨,理查德,感谢您的回复。你的第一个例子没有区别。你能让它发挥作用吗?您的第二个示例有效,但这适用于整个列表视图,因此我需要让您的第一个示例正常工作。
    • 恐怕我目前没有可用于测试的 Windows 8 机器。如果您没有事先得到答案,我会稍后再看。
    • 太棒了。感谢您的宝贵时间
    • 您可以为您的 ItemTemplate 发布 xaml 吗?
    • 理查德。我刚刚添加了项目模板
    【解决方案2】:

    对我来说效果很好,如下所示:

    <ListView.ItemContainerStyle>
      <Style TargetType="ListViewItem">
        <Style.Triggers>
          <Trigger Property="IsMouseOver" Value="True">
            <Setter Property="Background" Value="Transparent" />
            <Setter Property="BorderBrush" Value="Transparent" />
            <Setter Property="BorderThickness" Value="0" />
          </Trigger>
        </Style.Triggers>
      </Style>
    </ListView.ItemContainerStyle>
    

    【讨论】:

    • 请提供一些关于为什么这会起作用的信息,而不仅仅是说“这是好的代码”
    猜你喜欢
    • 2011-06-20
    • 2013-01-17
    • 1970-01-01
    • 2011-04-20
    • 1970-01-01
    • 2019-12-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多