This is because the routing strategy of the Loaded event is Direct, which means that the routed event does not route though an element tree. This is why we are unable to catch the Loaded event from the ListViewItems. You can refer to the doucment of Loaded event to get more information about this. The following example shows how to do this.

Code Block

namespace ForumProjects

{

    public partial class MainWindow : Window

    {

        public MainWindow()

        {

            this.Persons = new List<Person>()

            {

                new Person(){ID=1,Name="AAA",Comment="Comment AAA"},

                new Person(){ID=2,Name="BBB",Comment="Comment BBB"},

                new Person(){ID=3,Name="CCC",Comment="Comment CCC"},

                new Person(){ID=4,Name="DDD",Comment="Comment DDD"},

            };

            InitializeComponent();

        }

        public List<Person> Persons { get; private set; }

    }

    public class Person

    {

        public int ID { get; set; }

        public string Name { get; set; }

        public string Comment { get; set; }

    }

    public class PersonListView : ListView

    {

        protected override DependencyObject GetContainerForItemOverride()

        {

            return new PersonListViewItem();

        }

        protected override bool IsItemItsOwnContainerOverride(object item)

        {

            return item is PersonListViewItem;

        }

    }

    public class PersonListViewItem : ListViewItem

    {

        public static readonly DependencyProperty PopupTextProperty =

            DependencyProperty.Register("PopupText", typeof(string), typeof(PersonListViewItem), new FrameworkPropertyMetadata(PopupTextChanged));

        public static readonly DependencyProperty IsPopupOpenProperty =

            DependencyProperty.Register("IsPopupOpen", typeof(bool), typeof(PersonListViewItem), new FrameworkPropertyMetadata(IsPopupOpenChanged));

        private Popup popup;

        private TextBlock textBlock;

        public PersonListViewItem()

        {

            this.textBlock = new TextBlock();

            Grid grid = new Grid() { Background = Brushes.White };

            grid.Children.Add(this.textBlock);

            this.popup = new Popup() { Child = grid, PlacementTarget = this, Placement = PlacementMode.Right };

        }

        public string PopupText

        {

            get { return (string)GetValue(PopupTextProperty); }

            set { SetValue(PopupTextProperty, value); }

        }

        public bool IsPopupOpen

        {

            get { return (bool)GetValue(IsPopupOpenProperty); }

            set { SetValue(IsPopupOpenProperty, value); }

        }

        private static void IsPopupOpenChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)

        {

            PersonListViewItem item = d as PersonListViewItem;

            if (item != null)

            {

                item.popup.IsOpen = (bool)e.NewValue;

            }

        }

        private static void PopupTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)

        {

            PersonListViewItem item = d as PersonListViewItem;

            if (item != null)

            {

                item.textBlock.Text = (string)e.NewValue;

            }

        }

    }

}

<Window x:Class="ForumProjects.MainWindow"

       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

       xmlns:local="clr-namespace:ForumProjects"

       x:Name="Window" Title="MainWindow" Height="700" Width="800">

    <StackPanel>

        <local:PersonListView ItemsSource="{Binding ElementName=Window, Path=Persons}">

            <local:PersonListView.View>

                <GridView>

                    <GridViewColumn Header="ID" DisplayMemberBinding="{Binding ID}"/>

                    <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}"/>

                </GridView>

            </local:PersonListView.View>

            <local:PersonListView.ItemContainerStyle>

                <Style TargetType="local:PersonListViewItem">

                    <Style.Triggers>

                        <Trigger Property="IsMouseOver" Value="True">

                            <Setter Property="IsPopupOpen" Value="True"/>

                        </Trigger>

                    </Style.Triggers>

                    <Setter Property="PopupText" Value="{Binding Comment}"/>

                </Style>

            </local:PersonListView.ItemContainerStyle>

        </local:PersonListView>

    </StackPanel>

</Window>

Best Regards,

Wei Zhou

原文:WPF listview item mouse enter/over popup

 

WPF listview item mouse enter/over popup的更多相关文章

  1. C# Note16: wpf window 中添加enter和双击事件

     一.添加回车(enter)事件 在C#编程时,有时希望通过按回车键,控件焦点就会自动从一个控件跳转到下一个控件进行操作. 以用户登录为例,当输入完用户名和密码后, 需要点击登录按钮,而登录按钮必须获 ...

  2. WPF ListView 选中问题

    WPF ListView 选中问题  摘自:http://www.cnblogs.com/BBHor/archive/2013/04/28/VisualTreeHelper-PreviewMouseD ...

  3. WPF ListView控件设置奇偶行背景色交替变换以及ListViewItem鼠标悬停动画

    原文:WPF ListView控件设置奇偶行背景色交替变换以及ListViewItem鼠标悬停动画 利用WPF的ListView控件实现类似于Winform中DataGrid行背景色交替变换的效果,同 ...

  4. Android listview.item.clear()与listview.clear()的区别

    listview.clear()与listview.item.clear()的区别就是使用了listview.item.clear()后,listview控件中仍然保存着listviewitem项的结 ...

  5. ListView item 中TextView 如何获取长按事件

    昨天晚上小伙伴突然来信, ListView item中嵌套的TextView 无法获取长按事件 从前从来没有仔细留意过, coding后发现...果然没什么动静 而且没有合适的API让我调用获取Tex ...

  6. [WPF]ListView点击列头排序功能实现

    [转]   [WPF]ListView点击列头排序功能实现 这是一个非常常见的功能,要求也很简单,在Column Header上显示一个小三角表示表示现在是在哪个Header上的正序还是倒序就可以了. ...

  7. Android 原生listview item伸展收缩效果 (续)

    接上一个原生的listview item的伸展收缩效果. 上一个可能做的有些粗糙,效果也没有这个好,上代码. package com.example.listviewdemo; import java ...

  8. Android 原生listview item伸展收缩效果

    Android原生listview做的一个item的伸缩效果.*永远不要让你老大有机会改需求 package com.example.yunkanglast; import java.io.Seria ...

  9. android 修改listview item view 的方法(转)

    android 修改listview item view 的方法   具体的解答办法很简单: 代码如下 : 1.获取需要更新的view int visiblePosition = mListView. ...

随机推荐

  1. Java NIO详细介绍

    不错的文章,推荐一下. http://www.ibm.com/developerworks/cn/education/java/j-nio/j-nio.html JavaNIO非堵塞技术实际是采取Re ...

  2. js循环json数组

    例如数据库里面的json字符串是这样的 var str = '[{"name":"宗2瓜","num":"1",&quo ...

  3. deep learning实践经验总结

    近期拿caffe来做图片分类.遇到不少问题,同一时候也吸取不少教训和获得不少经验. 先看样例再总结经验. 这是一个2类分类器.分的是条纹衣服和纯色衣服. 先看几张图片. 条纹衣服:   纯色衣服: w ...

  4. Kolya and Tandem Repeat

     Kolya and Tandem Repeat time limit per test 2 seconds memory limit per test 256 megabytes input s ...

  5. 查看MySQL数据的连接

      show processlist;   select host from information_schema.processlist;   查看那台机器及连接数 select host, cur ...

  6. JAVA 安装JDK注意事项

    在JAVA jdk安装的时候,文件夹名称最好不要有中文,有逗号,有下划线,空格等问题.这样easy出错,比方以下几个安装路径easy出错    java文件夹/jdk1.6_x86/    这样的就是 ...

  7. Python实战:如何隐藏自己的爬虫身份

    使用爬虫访问网站,需要尽可能的隐藏自己的身份,以防被服务器屏蔽,在工作工程中,我们有2种方式来实现这一目的,分别是延时访问和动态代理,接下来我们会对这两种方式进行讲解 1.延时访问 见名之意,延时访问 ...

  8. Java相关思维导图分享

    非常多朋友都给我发私信希望获得一份Java知识的思维导图,我来不及一一答复.原先是给大家一个百度网盘的链接分享,大家能够自己去下载,可是不知道云盘还能用多久.把相关资源转移到了QQ的群共享中.须要的朋 ...

  9. WPF Chart 图标

    DevExpress: <dxc:ChartControl.Diagram> <dxc:XYDiagram2D.SeriesTemplate> </dxc:XYDiagr ...

  10. java读取.properties文件乱码

    1.config.properties文件写不进中文,写进去都变成了unicode,解决的方法是右键该文件--Properties--Resource--Text file encoding ,选ot ...