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

2 回答 2

16

编辑:

我能让这个工作的唯一方法是重新定义ListViewItem ControlTemplate. 试试下面的代码,看看它是否能解决您的问题:

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

    <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出于演示目的,我对颜色进行了硬编码。理想情况下,您会从资源文件中获取这些内容。

于 2013-06-19T10:46:35.790 回答
7

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

<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>
于 2016-06-11T10:27:18.113 回答