none
Popup for Listview

    Question

  • Goal:
    When you have the cursor in one of of the listview's row, a pop up, with text information, should display.

    If you select a new row in the listview a new text information should display.

    Problem:
    How should I do it? I cannot find a tutorial for it?

    Information:
    - The size of the popup is the same.
    - I'm using C# with WPF.

    Friday, June 20, 2014 8:53 PM

Answers

  • You could define a Popup element and display and hide this by handling the SelectionChanged event of the ListView and/or the MouseEnter and MouseLeave events of the ListViewItems:

            <ListView x:Name="lv" SelectionChanged="lv_SelectionChanged">
                <ListView.ItemContainerStyle>
                    <Style TargetType="ListViewItem">
                        <EventSetter Event="MouseEnter" Handler="OnMouseEnter"/>
                        <EventSetter Event="MouseLeave" Handler="OnMouseLeave"/>
                    </Style>
                </ListView.ItemContainerStyle>
                <ListView.View>
                    <GridView>
                        <GridViewColumn DisplayMemberBinding="{Binding}" Header="..."/>
                    </GridView>
                </ListView.View>
            </ListView>
            <Popup x:Name="popup">
                <Grid Background="Silver" Width="100" Height="100">
                    <TextBlock Text="{Binding}"/>
                </Grid>
            </Popup>

    private void lv_SelectionChanged(object sender, SelectionChangedEventArgs e)
            {
                ListViewItem lvi = lv.ItemContainerGenerator.ContainerFromItem(lv.SelectedItem) as ListViewItem;
                ShowPopup(lvi);
            }
    
            private void OnMouseEnter(object sender, MouseEventArgs e)
            {
                ListViewItem lvi = sender as ListViewItem;
                ShowPopup(lvi);
            }
    
            private void OnMouseLeave(object sender, MouseEventArgs e)
            {
                popup.IsOpen = false;
            }
    
            private void ShowPopup(ListViewItem lvi)
            {
                popup.IsOpen = false;
                popup.PlacementTarget = lvi;
                popup.Placement = PlacementMode.Center;
                popup.DataContext = lvi.DataContext;
                popup.IsOpen = true;
            }
    

    Sunday, June 22, 2014 7:49 PM