locked
Display PopUp window at MouseOver event on LIstViewItem RRS feed

  • Question

  • Hi All.

    I have ListView with tooltip at MouseOver event on ListViewItem. Now I would like to have popup form that include different tool controls with own binding sources and that will popup at MouseOver event on ListViewItem by the same way like tooltip. If that is possible I will appreciate for example and detail description how it to do.

    Thanks.


    • Edited by zleug Monday, November 28, 2011 3:55 PM
    Monday, November 28, 2011 3:44 PM

Answers

  • You should be able to do that by creating a method to retrieve the DataRow which contains the ID. Get_Ldiary_PopupToolTip will be something similar to this:

    String ManagerID = ReferralDataRow["ID"].ToString();
    popUpManager.Text = RetrieveData(ManagerID);
    
    
    

    Then your method will be something like this:

    public string RetrieveData(String ManagerID)
    {
    // Retrieve the DataRow which contains WHERE ID = "ManagerID"
    }


    Please me know if you have any other concerns.

    Otherwise, please take time to mark this thread as Answered if you have no more problems.

     

    Best Regards,

    Ldiary

     

     


    Need translation help? I translate from English to Japanese and vice versa. Please feel free to contact me for your translation needs. 日英・英日翻訳依頼を受け付けています。 お気軽にご相談下さい。(Ldiary.com)
    • Marked as answer by zleug Friday, December 2, 2011 5:49 PM
    • Unmarked as answer by zleug Friday, December 2, 2011 5:51 PM
    • Marked as answer by Sheldon _Xiao Monday, December 5, 2011 2:10 AM
    Friday, December 2, 2011 9:43 AM
  • Hi Ldiary.

    I very appreciate for help. Thanks a lot.

    • Marked as answer by zleug Friday, December 2, 2011 5:52 PM
    Friday, December 2, 2011 5:50 PM

All replies

    • Proposed as answer by Sheldon _Xiao Tuesday, November 29, 2011 7:03 AM
    Monday, November 28, 2011 4:48 PM
  • Here's how you can do it.

    1. Create a Popup element and place controls in it.

            <Popup  Height="Auto" Width="100" Name="listViewPopup" StaysOpen="True" AllowsTransparency="True"  >
                <Border BorderThickness="2" Background="ForestGreen" >
                    <StackPanel Margin="20"  Orientation="Horizontal">
                        <TextBlock Text="Age is "/>
                        <TextBlock  Name="popupTextBlock" />
                    </StackPanel>
                </Border>
            </Popup>

    2. In your ListView, add a Style in your ItemsContainerStyle. Then in that style put EventSetters for MouseEnter and for MouseLeave.

            <ListView Name="motherList" ItemsSource="{Binding}">
                <ListView.ItemContainerStyle>
                    <Style TargetType="{x:Type ListViewItem}">
                        <EventSetter Event="Mouse.MouseEnter" Handler="Get_Ldiary_PopupToolTip" />
                        <EventSetter Event="Mouse.MouseLeave" Handler="Remove_Ldiary_PopupToolTip"/>
                    </Style>
                </ListView.ItemContainerStyle>
                <ListView.View>
                    <GridView >
                        <GridView.Columns>
                            <GridViewColumn Header="First Name" DisplayMemberBinding="{Binding Path=First}" />
                            <GridViewColumn Header="Last Name" DisplayMemberBinding="{Binding Path=Last}" />
                        </GridView.Columns>
                    </GridView>
                </ListView.View>
            </ListView>

    3. In your code-behind, define the handlers for these MouseEnter and MouseLeave events.

            private void Get_Ldiary_PopupToolTip(object sender, MouseEventArgs e)
            {
                ListViewItem listViewItem = e.Source as ListViewItem;
                Person person = listViewItem.Content as Person;
                popupTextBlock.Text = person.Age;
                listViewPopup.PlacementTarget = listViewItem;
                listViewPopup.Placement = PlacementMode.MousePoint;
                listViewPopup.IsOpen = true;
            }
            private void Remove_Ldiary_PopupToolTip(object sender, MouseEventArgs e)
            {
                listViewPopup.IsOpen = false;
            }

     

    Here is a working sample you can try.

    <Window x:Class="LdiaryPopupToolTip.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="200" Width="250">
        <StackPanel Name="motherPanel">
            <Popup  Height="Auto" Width="100" Name="listViewPopup" StaysOpen="True" AllowsTransparency="True"  >
                <Border BorderThickness="2" Background="ForestGreen" >
                    <StackPanel Margin="20"  Orientation="Horizontal">
                        <TextBlock Text="Age is "/>
                        <TextBlock  Name="popupTextBlock" />
                    </StackPanel>
                </Border>
            </Popup>
            <ListView Name="motherList" ItemsSource="{Binding}">
                <ListView.ItemContainerStyle>
                    <Style TargetType="{x:Type ListViewItem}">
                        <EventSetter Event="Mouse.MouseEnter" Handler="Get_Ldiary_PopupToolTip" />
                        <EventSetter Event="Mouse.MouseLeave" Handler="Remove_Ldiary_PopupToolTip"/>
                    </Style>
                </ListView.ItemContainerStyle>
                <ListView.View>
                    <GridView >
                        <GridView.Columns>
                            <GridViewColumn Header="First Name" DisplayMemberBinding="{Binding Path=First}" />
                            <GridViewColumn Header="Last Name" DisplayMemberBinding="{Binding Path=Last}" />
                        </GridView.Columns>
                    </GridView>
                </ListView.View>
            </ListView>
        </StackPanel>
    </Window>
    

     

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
    using System.Windows.Controls.Primitives;
     
    namespace LdiaryPopupToolTip
    {
        /// <summary>
        /// Interaction logic for Window1.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
                List<Person> lst = new List<Person>();
                Person ps = new Person();
                ps.First = "Ldiary";
                ps.Last = "Translations";
                ps.Age = "21";
                lst.Add(ps);
                ps = new Person();
                ps.First = "L";
                ps.Last = "English";
                ps.Age = "30";
                lst.Add(ps);
                ps = new Person();
                ps.First = "Diary";
                ps.Last = "Japanese";
                ps.Age = "19";
                lst.Add(ps);
                motherPanel.DataContext = lst;
            }
     
            private void Get_Ldiary_PopupToolTip(object sender, MouseEventArgs e)
            {
                ListViewItem listViewItem = e.Source as ListViewItem;
                Person person = listViewItem.Content as Person;
                popupTextBlock.Text = person.Age;
                listViewPopup.PlacementTarget = listViewItem;
                listViewPopup.Placement = PlacementMode.MousePoint;
                listViewPopup.IsOpen = true;
            }
            private void Remove_Ldiary_PopupToolTip(object sender, MouseEventArgs e)
            {
                listViewPopup.IsOpen = false;
            }
     
        }
     
        public class Person
        {
            private string _first;
            public string First
            {
                get { return _first; }
                set { _first = value; }
            }
            private string _last;
            public string Last
            {
                get { return _last; }
                set { _last = value; }
            }
            private string _age;
            public string Age
            {
                get { return _age; }
                set { _age = value; }
            }
        }
    }
    

     


    Need translation help? I translate from English to Japanese and vice versa. Please feel free to contact me for your translation needs. 日英・英日翻訳依頼を受け付けています。 お気軽にご相談下さい。(Ldiary.com)
    • Proposed as answer by Sheldon _Xiao Tuesday, November 29, 2011 7:03 AM
    Monday, November 28, 2011 7:29 PM
  • Hi Ldiary. Thanks for replay.

    I tried to modify your sample to my code. My XAML looks:

            <Popup  Height="Auto" Width="100" Name="listViewPopup" StaysOpen="True" AllowsTransparency="True"  >
                <Border BorderThickness="2" Background="ForestGreen" >
                    <StackPanel Margin="20"  Orientation="Horizontal">
    	            <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="80" />
                            <ColumnDefinition Width="190" />
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="23" />
                            <RowDefinition Height="23" />
                            <RowDefinition Height="23" />
                       </Grid.RowDefinitions>
                       <TextBlock Name="lblProf" Text="Physician" VerticalAlignment="Center" />
                      <TextBlock Name="ProfText" Margin="0,6,0,6" Grid.Column="1" Foreground="DarkBlue" Visibility="Visible" FontWeight="Bold" HorizontalAlignment="Left" Width="0"></TextBlock>
                      <TextBlock Name="lblManager" Text="Care Manager" Grid.Row="1" VerticalAlignment="Center" />
                      <TextBlock Name="ManagerText" Margin="0,6,0,6" Grid.Row="1" Grid.Column="1" Foreground="DarkBlue" Visibility="Visible" FontWeight="Bold" HorizontalAlignment="Left" Width="0"></TextBlock>
                      <TextBlock Name="lblOffice" Text="Clinic" Grid.Row="2" VerticalAlignment="Center" />
                      <TextBlock Name="OfficeText" Margin="0,6,0,6" Grid.Row="2" Grid.Column="1" Foreground="DarkBlue" Visibility="Visible" FontWeight="Bold" HorizontalAlignment="Left" Width="0"></TextBlock>
                    </StackPanel>
                </Border>
            </Popup>
            <ListView Name="TestListView" Background="#f8f8FF" BorderThickness="0" 
                        ItemsSource="{Binding}" Visibility="Visible" SelectedValuePath="Test_Id" Height="Auto" 
                        IsSynchronizedWithCurrentItem="True" 
                        VerticalContentAlignment="Center" FontWeight="Normal" FontFamily="Tahoma" FontSize="12" 
                        Grid.IsSharedSizeScope="False" SelectionMode="Single">
                <ListView.ItemContainerStyle>
                    <Style TargetType="{x:Type ListViewItem}">
                        <EventSetter Event="Mouse.MouseEnter" Handler="Get_Ldiary_PopupToolTip" />
                        <EventSetter Event="Mouse.MouseLeave" Handler="Remove_Ldiary_PopupToolTip"/>
                    </Style>
                </ListView.ItemContainerStyle>
                <ListView.View>
                    <GridView AllowsColumnReorder="True" ColumnHeaderContainerStyle="{StaticResource HeaderStyle}">
                        <GridViewColumn Header="Type" CellTemplate="{StaticResource Type}"  Width="50"/>
                        <GridViewColumn Header="TestDate" DisplayMemberBinding="{Binding Path=TestDate}" Width="90"/>
                        <GridViewColumn Header="Team" DisplayMemberBinding="{Binding Path=Team}" Width="140"/>
                        <GridViewColumn Header="Pat No" DisplayMemberBinding="{Binding Path=PatNo}" Width="90"/>
                        <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Path=Name}" Width="220"/>
                        <GridViewColumn Header="Status" DisplayMemberBinding="{Binding Path=Status}" Width="90"/>
                    </GridView>
                </ListView.View>
            </ListView>
    
    For code instead of class Person I fill LIstView by binding it to DataSet TableAdapter
        Private Sub Window2_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
            Me.TestlListTableAdapter.Fillby(Me.MyDataSet.TestList)
            Me.cvTestList = CollectionViewSource.GetDefaultView(Me.MyDataSet.TestList)
            Me.TestListView.DataContext = Me.cvTestList
        End Sub
    
    

    In that case how to modify procedure Get_Ldiary_PopupToolTip if ListView binging DataSet TableAdapter.

    Thanks.

    Tuesday, November 29, 2011 11:10 PM
  • I don't know much about Visual Basic so I'll answer in C#.  You just need to comment out the two lines below:

            private void Get_Ldiary_PopupToolTip(object sender, MouseEventArgs e)
            {
                ListViewItem listViewItem = e.Source as ListViewItem;
                //Person person = listViewItem.Content as Person;
                //popupTextBlock.Text = person.Age;
                listViewPopup.PlacementTarget = listViewItem;
                listViewPopup.Placement = PlacementMode.MousePoint;
                listViewPopup.IsOpen = true;
            }

     

    Since you are not passing data to the Popup, you don't need to get "person.Age" and "popupTextBlock".

     


    Need translation help? I translate from English to Japanese and vice versa. Please feel free to contact me for your translation needs. 日英・英日翻訳依頼を受け付けています。 お気軽にご相談下さい。(Ldiary.com)
    • Edited by Ldiary Wednesday, November 30, 2011 2:56 AM
    Wednesday, November 30, 2011 2:55 AM
  • For code instead of class Person I fill LIstView by binding it to DataSet TableAdapter

    Sorry, I think I overlooked this one.  Since you are populating the DataGrid with a DataTable (called TestList in your code), you will need to cast the

    listViewItem.Content

    into a DataRow.  It may be something like this:

    DataRow dataRow = listViewItem.Content as DataRow;

    and then you can access the particular cell/column you want by passing in the name of your column. Someting like this:

    popupTextBlock.Text = daraRow["YourColumnName"];

    Let me know if you continue to have problems with this.


    Need translation help? I translate from English to Japanese and vice versa. Please feel free to contact me for your translation needs. 日英・英日翻訳依頼を受け付けています。 お気軽にご相談下さい。(Ldiary.com)
    Wednesday, November 30, 2011 2:29 PM
  • Hi Ldiary.

    Now the procudere Get_Ldiary_PopupToolTip looks like:

        Private Sub Get_Ldiary_PopupToolTip(ByVal sender As Object, ByVal e As MouseEventArgs)
            Dim itemList As ListViewItem
            Dim ListDataRow As System.Data.DataRowView
            Dim Manager As String
    
            itemList = CType(sender, ListViewItem)
            ListDataRow = itemList.Content
            Manager = ListDataRow("Manager")
            listViewPopup.PlacementTarget = itemList
            listViewPopup.Placement = PlacementMode.MousePoint
            listViewPopup.IsOpen = True
        End Sub

     

    But Popup didn't display value Manager.

    Thanks.





    • Edited by zleug Wednesday, November 30, 2011 4:52 PM
    Wednesday, November 30, 2011 4:48 PM
  • Try casting the

    e.Source
    

    into a ListViewItem and not the sender.

    itemReferral = CType(e.Source, ListViewItem)
    

     


    Need translation help? I translate from English to Japanese and vice versa. Please feel free to contact me for your translation needs. 日英・英日翻訳依頼を受け付けています。 お気軽にご相談下さい。(Ldiary.com)
    Wednesday, November 30, 2011 4:54 PM
  • Also, don't forget that you will need

    ManagerText.Text = CareManager;

    to be able to pass the CareManager string into the popup.


    Need translation help? I translate from English to Japanese and vice versa. Please feel free to contact me for your translation needs. 日英・英日翻訳依頼を受け付けています。 お気軽にご相談下さい。(Ldiary.com)
    Wednesday, November 30, 2011 4:58 PM
  • I got error message: Text is not a member of 'String'.

    Thanks.
    Wednesday, November 30, 2011 5:22 PM
  • The ManagerText is the name you have in your xaml

     <TextBlock Name="ManagerText" 

    did you changed that name?


    Need translation help? I translate from English to Japanese and vice versa. Please feel free to contact me for your translation needs. 日英・英日翻訳依頼を受け付けています。 お気軽にご相談下さい。(Ldiary.com)
    Wednesday, November 30, 2011 5:28 PM
  • XAML

    <TextBlock Name="Manager" Margin="0,6,0,6" Grid.Row="1" Grid.Column="1" Foreground="DarkBlue" Visibility="Visible" FontWeight="Bold" HorizontalAlignment="Left" Width="0"></TextBlock>
    

    Code

    Manager.Text = ReferralDataRow("Manager") or I tried Manager.Text = Manager
    

    I get error message: Text is not a member of 'String'. on left part of that expression.

    Thanks.



    • Edited by zleug Wednesday, November 30, 2011 5:50 PM
    Wednesday, November 30, 2011 5:49 PM
  • Perhaps you are having conflicts with the Manager string you declared, because they both have the same name, this:

    <TextBlock Name="Manager" 

    and this:

    Dim Manager As String
    

    So change the name of the TextBlock to something like this:

    <TextBlock Name="popUpManager" 

    and then do this:

    popUpManager.Text = ReferralDataRow("Manager") 

    see if the error goes away.


    Need translation help? I translate from English to Japanese and vice versa. Please feel free to contact me for your translation needs. 日英・英日翻訳依頼を受け付けています。 お気軽にご相談下さい。(Ldiary.com)
    Wednesday, November 30, 2011 5:54 PM
  • Yeap, unfortunately.

    After I removed line in code: Dim Manager As String

    and rename in XAML Manager to <TextBlock Name="popUpManager"

    like you suggested I got new error message:  "Conversion from type 'DBNull' to type 'String' is not valid." in line:

    popUpManager.Text = ReferralDataRow("Manager")

    I changed column name Manager to column that don't has NULL values and for debuging inserted message box like: 

    MsgBox(ReferralDataRow("RegNo")) so the MsgBox display values on MouseOver event but Popup still is empty. How to prevent error message if cursor on MouseOver and value is Null?

    Thanks.


    • Edited by zleug Wednesday, November 30, 2011 8:12 PM
    Wednesday, November 30, 2011 7:24 PM
  • That means

    ReferralDataRow("Manager")

    is returning a null. Debug your code starting from

    itemList = CType(e.Source, ListViewItem)
            ListDataRow = itemList.Content
    

    and make sure you are using the correct variable names.


    Need translation help? I translate from English to Japanese and vice versa. Please feel free to contact me for your translation needs. 日英・英日翻訳依頼を受け付けています。 お気軽にご相談下さい。(Ldiary.com)
    Wednesday, November 30, 2011 7:29 PM
  • Error message fixed by:

    popUpManager.Text = ReferralDataRow("Manager").ToString

    Wednesday, November 30, 2011 10:14 PM
  • Okay, very good. Now did the correct data appeared on the Popup?

     


    Need translation help? I translate from English to Japanese and vice versa. Please feel free to contact me for your translation needs. 日英・英日翻訳依頼を受け付けています。 お気軽にご相談下さい。(Ldiary.com)
    Thursday, December 1, 2011 2:40 AM
  • Can you explain what are Sender and e.Source? And where can I read how to use?

    Thanks

    Thursday, December 1, 2011 5:50 AM
  • Is your problem about popup solved already?

     

    You can find explanation about sender and source in this link:

    Routed Events Overview

    A routed event handler delegate provides references to two objects: the object that raised the event and the object where the handler was invoked. The object where the handler was invoked is the object reported by the sender parameter. The object where the event was first raised is reported by the Source property in the event data. A routed event can still be raised and handled by the same object, in which case sender and Source are identical.


    Need translation help? I translate from English to Japanese and vice versa. Please feel free to contact me for your translation needs. 日英・英日翻訳依頼を受け付けています。 お気軽にご相談下さい。(Ldiary.com)
    • Edited by Ldiary Thursday, December 1, 2011 6:19 AM
    Thursday, December 1, 2011 6:18 AM
  • Hi Ldiary.

    The popup is display Manager value on ListViewItem MouseOver event. Is it possible to display popup value if I source for Popup and ListView control have separate data source (TableAdapters). But both sources have same ID field? If yes, how to modify Get_Ldiary_PopupToolTip procedure in that case?

    Thanks.




    • Edited by zleug Thursday, December 1, 2011 9:08 PM
    Thursday, December 1, 2011 8:34 PM
  • You should be able to do that by creating a method to retrieve the DataRow which contains the ID. Get_Ldiary_PopupToolTip will be something similar to this:

    String ManagerID = ReferralDataRow["ID"].ToString();
    popUpManager.Text = RetrieveData(ManagerID);
    
    
    

    Then your method will be something like this:

    public string RetrieveData(String ManagerID)
    {
    // Retrieve the DataRow which contains WHERE ID = "ManagerID"
    }


    Please me know if you have any other concerns.

    Otherwise, please take time to mark this thread as Answered if you have no more problems.

     

    Best Regards,

    Ldiary

     

     


    Need translation help? I translate from English to Japanese and vice versa. Please feel free to contact me for your translation needs. 日英・英日翻訳依頼を受け付けています。 お気軽にご相談下さい。(Ldiary.com)
    • Marked as answer by zleug Friday, December 2, 2011 5:49 PM
    • Unmarked as answer by zleug Friday, December 2, 2011 5:51 PM
    • Marked as answer by Sheldon _Xiao Monday, December 5, 2011 2:10 AM
    Friday, December 2, 2011 9:43 AM
  • Hi Ldiary.

    I very appreciate for help. Thanks a lot.

    • Marked as answer by zleug Friday, December 2, 2011 5:52 PM
    Friday, December 2, 2011 5:50 PM