WPF listview item mouse enter/over popup

本文介绍如何在WPF中实现ListViewItem鼠标悬停时显示弹出框的功能。通过自定义ListViewItem,并利用依赖属性来控制弹出框的显示与隐藏。
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
 
 
posted on 2018-08-14 10:51 NET未来之路 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/lonelyxmas/p/9473222.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值