1. 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.
  2.  
  3. Code Block
  4.  
  5. namespace ForumProjects
  6.  
  7. {
  8.  
  9. public partial class MainWindow : Window
  10.  
  11. {
  12.  
  13. public MainWindow()
  14.  
  15. {
  16.  
  17. this.Persons = new List<Person>()
  18.  
  19. {
  20.  
  21. new Person(){ID=1,Name="AAA",Comment="Comment AAA"},
  22.  
  23. new Person(){ID=2,Name="BBB",Comment="Comment BBB"},
  24.  
  25. new Person(){ID=3,Name="CCC",Comment="Comment CCC"},
  26.  
  27. new Person(){ID=4,Name="DDD",Comment="Comment DDD"},
  28.  
  29. };
  30.  
  31. InitializeComponent();
  32.  
  33. }
  34.  
  35. public List<Person> Persons { get; private set; }
  36.  
  37. }
  38.  
  39. public class Person
  40.  
  41. {
  42.  
  43. public int ID { get; set; }
  44.  
  45. public string Name { get; set; }
  46.  
  47. public string Comment { get; set; }
  48.  
  49. }
  50.  
  51. public class PersonListView : ListView
  52.  
  53. {
  54.  
  55. protected override DependencyObject GetContainerForItemOverride()
  56.  
  57. {
  58.  
  59. return new PersonListViewItem();
  60.  
  61. }
  62.  
  63. protected override bool IsItemItsOwnContainerOverride(object item)
  64.  
  65. {
  66.  
  67. return item is PersonListViewItem;
  68.  
  69. }
  70.  
  71. }
  72.  
  73. public class PersonListViewItem : ListViewItem
  74.  
  75. {
  76.  
  77. public static readonly DependencyProperty PopupTextProperty =
  78.  
  79. DependencyProperty.Register("PopupText", typeof(string), typeof(PersonListViewItem), new FrameworkPropertyMetadata(PopupTextChanged));
  80.  
  81. public static readonly DependencyProperty IsPopupOpenProperty =
  82.  
  83. DependencyProperty.Register("IsPopupOpen", typeof(bool), typeof(PersonListViewItem), new FrameworkPropertyMetadata(IsPopupOpenChanged));
  84.  
  85. private Popup popup;
  86.  
  87. private TextBlock textBlock;
  88.  
  89. public PersonListViewItem()
  90.  
  91. {
  92.  
  93. this.textBlock = new TextBlock();
  94.  
  95. Grid grid = new Grid() { Background = Brushes.White };
  96.  
  97. grid.Children.Add(this.textBlock);
  98.  
  99. this.popup = new Popup() { Child = grid, PlacementTarget = this, Placement = PlacementMode.Right };
  100.  
  101. }
  102.  
  103. public string PopupText
  104.  
  105. {
  106.  
  107. get { return (string)GetValue(PopupTextProperty); }
  108.  
  109. set { SetValue(PopupTextProperty, value); }
  110.  
  111. }
  112.  
  113. public bool IsPopupOpen
  114.  
  115. {
  116.  
  117. get { return (bool)GetValue(IsPopupOpenProperty); }
  118.  
  119. set { SetValue(IsPopupOpenProperty, value); }
  120.  
  121. }
  122.  
  123. private static void IsPopupOpenChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  124.  
  125. {
  126.  
  127. PersonListViewItem item = d as PersonListViewItem;
  128.  
  129. if (item != null)
  130.  
  131. {
  132.  
  133. item.popup.IsOpen = (bool)e.NewValue;
  134.  
  135. }
  136.  
  137. }
  138.  
  139. private static void PopupTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  140.  
  141. {
  142.  
  143. PersonListViewItem item = d as PersonListViewItem;
  144.  
  145. if (item != null)
  146.  
  147. {
  148.  
  149. item.textBlock.Text = (string)e.NewValue;
  150.  
  151. }
  152.  
  153. }
  154.  
  155. }
  156.  
  157. }
  158.  
  159. <Window x:Class="ForumProjects.MainWindow"
  160.  
  161. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  162.  
  163. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  164.  
  165. xmlns:local="clr-namespace:ForumProjects"
  166.  
  167. x:Name="Window" Title="MainWindow" Height="700" Width="800">
  168.  
  169. <StackPanel>
  170.  
  171. <local:PersonListView ItemsSource="{Binding ElementName=Window, Path=Persons}">
  172.  
  173. <local:PersonListView.View>
  174.  
  175. <GridView>
  176.  
  177. <GridViewColumn Header="ID" DisplayMemberBinding="{Binding ID}"/>
  178.  
  179. <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}"/>
  180.  
  181. </GridView>
  182.  
  183. </local:PersonListView.View>
  184.  
  185. <local:PersonListView.ItemContainerStyle>
  186.  
  187. <Style TargetType="local:PersonListViewItem">
  188.  
  189. <Style.Triggers>
  190.  
  191. <Trigger Property="IsMouseOver" Value="True">
  192.  
  193. <Setter Property="IsPopupOpen" Value="True"/>
  194.  
  195. </Trigger>
  196.  
  197. </Style.Triggers>
  198.  
  199. <Setter Property="PopupText" Value="{Binding Comment}"/>
  200.  
  201. </Style>
  202.  
  203. </local:PersonListView.ItemContainerStyle>
  204.  
  205. </local:PersonListView>
  206.  
  207. </StackPanel>
  208.  
  209. </Window>
  210.  
  211. Best Regards,
  212.  
  213. 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. C语言实现字符串截取函数left、mid和right

    作者:iamlaosong C语言字符串截取须要自己编程实现,只是.网络时代,自然不用自己从头写了.网上各种方法的实现代码已经多如牛毛了,这儿抄录一个感觉不错的备案. #include <std ...

  2. [Angular] Router outlet events

    For example, we have a component which just simply render router-outlet: import { Component } from ' ...

  3. 在Eclipse上打包并使用Proguard工具混淆jar包

    近期由于工作须要,学习到了Android jar包的打包与混淆. 之前觉得还是非常easy的,可是自己深入研究下,发现还是有一些东西须要注意的,并且自己也踩了一些坑,在这里写下供同僚们借鉴借鉴. 转载 ...

  4. php课程 4-15 数组遍历、超全局数组、表单提交数据(多看学习视频)

    php课程 4-15  数组遍历.超全局数组.表单提交数据(多看学习视频) 一.总结 一句话总结:超全局数组特别有用,比如$_SERVER可以获取所有的客户端访问服务器的情况. 1.数组遍历三种方式( ...

  5. 前端切图:调用百度地图API

    原型图 图片发自简书App <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> ...

  6. OC常用数据类型大全解

    UI基础 OC常用数据类型 Block Block封装了一段代码,可以在任何时候执行 Block可以作为函数参数或者函数的返回值,而其本身又可以带输入参数或返回值.它和传统的函数指针很类似,但是有区别 ...

  7. 记录一次对接XX支付SDK过程中报错问题

    我们支付平台以前我不做对接上游的,偶然间替别人做"对接了XX支付的相关接口的工作".在工作过程中发现SDK和对外提供服务过程中很容易出问题.在此做个记录,为了以后相关工作中作为自己 ...

  8. CocoaPods详解之(一)----使用篇

    CocoaPods详解之----使用篇 作者:wangzz 原文地址:http://blog.csdn.net/wzzvictory/article/details/18737437 一.什么是Coc ...

  9. webpack run dev后并没有生成dist目录,但是浏览器里却读取了dist里的build.js?

    最近想看看现在做的React项目用的脚手架,看了下webpack的配置,尝试修改一些东西看看输出结果,结果允许npm run dev发现没有输出目录,怎么回事呢.又安装了vue官方提供的webpack ...

  10. vscode 如何格式化vue(template)html代码 , 保持标签属性不换行

    微软的vscode 真心强大 , electron 框架写的 , 用js写的桌面应用 , 有能力的话大家可以分析一下人家的源码 , 反正我是看不了 , 太牛掰了 在一次跟新后我发现莫名奇妙的些在组件( ...