locked
Listbox Click RRS feed

  • Question

  • Well this seems like a stupid question but I cannot figure it out.  So here it goes.  I need to know when the user has clicked on an item in the listbox.  I have tried SelectionChanged and MouseLeftButtonUp.  But neither one of them works 100%.  Here is the problem with each of them:

    SelectionChanged - It will not register if you click on the item that is already selected.

    MouseLeftButtonUp - It will let you select a the selected item but it also fires off when you use the scroll bar.  I do nto want that.

    So, is there a way to see if the user just clicks on a single item in the listbox even if it is already selected?

     Thanks

    Thursday, May 29, 2008 5:17 PM

Answers

  • If you set up ItemTemplate and put a TextBlock as listbox item then you can hook up MouseLeftButtonDown event for that item.

    One thing you need to aware of if you do it this way: Textblock in Listbox always has the length of the Text no matter if you set HorizontalAlignment = "Stretch" (I think this is a bug).  So when you click the item it won't fire MouseLeftButtonDown event unless you click right on the Text. Doing following should avoid this:

    <ListBox x:Name="MyList" >                                            
                            <ListBox.ItemTemplate>                            
                                <DataTemplate>                                                                   
                                    <Grid Background="Transparent"  Cursor="Hand" Loaded="Grid_Loaded" MouseLeftButtonDown="Grid_MouseLeftButtonDown">                                         
                                        <TextBlock Text="{Binding Text}"/>                                
                                    </Grid>                                   
                                </DataTemplate>
                            </ListBox.ItemTemplate>                          
                        </ListBox>
     

    private void Grid_Loaded(object sender, RoutedEventArgs e)
            {
                FrameworkElement g = sender as FrameworkElement;
                g.Width = Menu.ActualWidth;
            }

            private void Grid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
            {
                
            }
     

     

     

     

    Thursday, May 29, 2008 8:24 PM

All replies

  • If you set up ItemTemplate and put a TextBlock as listbox item then you can hook up MouseLeftButtonDown event for that item.

    One thing you need to aware of if you do it this way: Textblock in Listbox always has the length of the Text no matter if you set HorizontalAlignment = "Stretch" (I think this is a bug).  So when you click the item it won't fire MouseLeftButtonDown event unless you click right on the Text. Doing following should avoid this:

    <ListBox x:Name="MyList" >                                            
                            <ListBox.ItemTemplate>                            
                                <DataTemplate>                                                                   
                                    <Grid Background="Transparent"  Cursor="Hand" Loaded="Grid_Loaded" MouseLeftButtonDown="Grid_MouseLeftButtonDown">                                         
                                        <TextBlock Text="{Binding Text}"/>                                
                                    </Grid>                                   
                                </DataTemplate>
                            </ListBox.ItemTemplate>                          
                        </ListBox>
     

    private void Grid_Loaded(object sender, RoutedEventArgs e)
            {
                FrameworkElement g = sender as FrameworkElement;
                g.Width = Menu.ActualWidth;
            }

            private void Grid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
            {
                
            }
     

     

     

     

    Thursday, May 29, 2008 8:24 PM
  • How do I know which item is MouseDown?

    Thursday, December 11, 2008 4:00 AM
  •  use atribut SelectedIndex - you get index (number) of the item wich was select.

    Thursday, December 11, 2008 4:23 AM