WPF listview item mouse enter/over popup

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

none
 
 
目录
相关文章
|
5月前
|
前端开发 C#
浅谈WPF之Popup弹出层
在日常开发中,当点击某控件时,经常看到一些弹出框,停靠在某些页面元素的附近,但这些又不是真正的窗口,而是页面的一部分,那这种功能是如何实现的呢?今天就以一个简单的小例子,简述如何在WPF开发中,通过Popup实现鼠标点击弹出浮动停靠窗口,仅供学习分享使用,如有不足之处,还请指正。
153 0
|
C#
【WPF】使用Popup控件做浮窗/提示框
原文:【WPF】使用Popup控件做浮窗/提示框 需求:当鼠标移入某个区域时,弹出一个浮窗,以便用户进行下一步操作。 效果如下图: 当鼠标移入左上角的【多选显示】框内,出现下面的浮窗(悬浮在原UI之上)。
4401 0
|
C#
关于WPF中Popup中的一些用法的总结
原文:关于WPF中Popup中的一些用法的总结   Popup控件是一个常用的非常有用的控件,顾明思义就是弹出式控件,首先我们来看看MSDN对它的解释吧,表示具有内容的弹出窗口,这个是非常重要的控件,我们看看它的继承关系吧:  System.
1498 0
|
C#
【WPF】拖拽ListBox中的Item
原文:【WPF】拖拽ListBox中的Item 整理了两个关于WPF拖拽ListBox中的Item的功能。项目地址 https://github.com/Guxin233/WPF-DragItemInListBox 需求一: 两个ListBox,拖拽其中一个ListBox的Item,放置到另一个ListBox中。
1615 0
|
C#
关于wpf中popup跟随鼠标移动显示
原文:关于wpf中popup跟随鼠标移动显示 最近在做一个画图工具,里面有一个功能是需要实现,当鼠标移动的时候在,鼠标的旁边显示坐标信息。 第一反应是想到了tooltip,但是tooltip有许多的限制,查询资料的过程中看到了popup,popup比tooltip更加灵活,下面讲讲tooltip跟popup的区别: 1.tooltip会自动显示,自动隐藏,而popup则需要设置IsOpen属性,并且在Popup.StaysOen属性为true时,Popup控件会一直显示,直到显式地将IsOpen属性设置为False。
1994 0
|
C#
WPF中Popup的几个问题
原文:WPF中Popup的几个问题 要用popup控件来解决一些问题。就此带来了一批问题。 问题一、 在popup外任意位置点击时要能关闭popup,这个本来简单,只要加上StaysOpen=false就可以了。
1394 0
|
前端开发 C#
WPF备忘录(1)有笑脸,有Popup
原文:WPF备忘录(1)有笑脸,有Popup 1.画个笑脸给大家娱乐一下: 效果如下:   2.
952 0
|
C#
WPF中Popup等弹窗的位置不对(偏左或者偏右)
原文:WPF中Popup等弹窗的位置不对(偏左或者偏右) 1.情况如图:    正常情况:         部分特殊情况:        在一般的电脑都能正确显示,就是第一种情况,同样的代码为什么在不同的电脑就会显示不同的位置呢,原来Windows为了满足 不同需求的用户,左撇子和右撇子,就...
1856 0
|
C#
WPF Popup 控件导致被遮挡内容不刷新的原因
原文:WPF Popup 控件导致被遮挡内容不刷新的原因                                    WPF Popup 控件导致被遮挡内容不刷新的原因                    周银辉   今天在写一个WPF控件时用到了Popup控件,很郁闷的情况是:当...
1108 0