WPF listview项会触发无限的MouseEnter和MouseLeave事件

时间:2016-12-30 06:46:54

标签: c# wpf

我有列表视图,其中包含一些项目,一个带有标题和描述的图像。 我必须实现一项功能,以便在用户 mouseenter 时在列表项目的图像区域中显示带有图像的悬停。

有没有正常工作,但有一个问题。 每当我将鼠标悬停在任何图像上时,悬停的图像会在下方闪烁,将无限次调用 mouseenter mouseleave 事件。

我用Google搜索并找到了SCOTT HANSELMAN's blog post,但没有任何对我有用。

<ListView ItemsSource="{Binding Path=CurrentSlideItems}" Name="listSlides" SelectionChanged="listSlides_SelectionChanged" KeyDown="listSlides_KeyDown" Padding="0" SelectionMode="Multiple" PreviewMouseDoubleClick="listSlides_PreviewMouseDoubleClick" Grid.ColumnSpan="2">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <Grid MaxWidth="{Binding Path=ActualWidth,ElementName=listSlides}">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="0.4*"/>
                            <ColumnDefinition Width="0.6*"/>
                        </Grid.ColumnDefinitions>

                        <Border Grid.Column="0" BorderBrush="Black" BorderThickness="1,1,1,1" Margin="0,2">
                            <Image Source="{Binding Path=Image}" MaxWidth="150" Name="image1" Stretch="Fill" Margin="0" MouseEnter="image1_MouseEnter" MouseLeave="image1_MouseLeave">
                            </Image>
                        </Border>

                        <StackPanel Orientation="Vertical" Grid.Column="1">
                            <TextBlock FontWeight="Bold" Text="{Binding Path=Title}" Margin="5,4,2,2" TextWrapping="Wrap" TextTrimming="WordEllipsis"/>
                            <TextBlock Text="{Binding Path=Description}" Margin="5,2,2,2" TextWrapping="Wrap" MaxHeight="80" TextTrimming="CharacterEllipsis" />
                        </StackPanel>
                    </Grid>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

编辑:包含活动

 private void image1_MouseEnter(object sender, MouseEventArgs e)
    {
        var srcImg = e.Source as System.Windows.Controls.Image;
        showPopup(srcImg);

    }

    private void image1_MouseLeave(object sender, MouseEventArgs e)
    {
        var pop = (Popup)this.FindResource("popup");

        if (!pop.IsMouseCaptured)
        {
            if (!popupFlag)
            {
                pop.IsOpen = false;
            }
        }
    }

0 个答案:

没有答案