locked
How to highlight a listbox item on right-click? RRS feed

  • Question

  • I want to right-click on an item in a ListBox control and have the item become the selected one. I don't seem to see a way to HitTest the control (if that is even the way to do it). An example would be appreciated.
    Friday, July 2, 2010 2:52 AM

Answers

  • Add this

     <ListBox.ItemContainerStyle>
                        <Style TargetType="ListBoxItem">
                            <Setter Property="HorizontalContentAlignment"
                                    Value="Stretch"></Setter>
                        </Style>
                    </ListBox.ItemContainerStyle>
    after this
    <ListBox x:Name="myListBox">
    Friday, July 2, 2010 7:10 PM

All replies

  • Are you using SL 4?

    Friday, July 2, 2010 4:24 AM
  • Here is an SL 4 solution. Xaml - thing to notice here is the right mouse button down event:

    <ListBox x:Name="myListBox">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <StackPanel Orientation="Vertical"
                                        MouseRightButtonDown="myListBox_MouseRightButtonDown">
                                <TextBlock Padding="5,0,5,0"
                                           Text="{Binding Column1}" />
                                <TextBlock Text="{Binding Column2}" />
                                <TextBlock Text="{Binding Column3}" />
                            </StackPanel>
                        </DataTemplate>
                    </ListBox.ItemTemplate>

                </ListBox>

     Code - two things here: e.Handled=true prevents event bubbling. And essentially you are using the data context to retrieve the bound item and make it the selected item.

            private void myListBox_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
            {
                e.Handled = true;
                FakeData fdItem = (FakeData)((StackPanel)sender).DataContext;
                myListBox.SelectedItem = fdItem;
            }
     
    Friday, July 2, 2010 4:54 AM
  • @snelldl  Thanks for your example, however, I cannot make it work.  Several issues:

    • You have MouseRightButtonDown event defined on the StackPanel.  It does not fire.  Neither does the MouseRightButtonUp event.  So I wired these events onto the ListBox and those do fire.
    • I set the data for the listbox in the following manner:

                  listBox1.ItemsSource = new MainModel().Items;  // this is an ObservableCollection<FakeData>

               The list does show up, but when the MouseRightButtonDown fires, (FakeData)((ListBox)sender).DataContext is null.  This makes sense, because I set the ItemsSource, not the DataContext.  If I set the .DataContext instead, I don't even see the list.

     So how do I resolve this? 


    Friday, July 2, 2010 5:12 PM
  • Yes, using SL4.

    Friday, July 2, 2010 5:13 PM
  • When you set the ItemsSource, the data template automatically creates the DataContext for each control in there. I don't know why it didn't work for you - I tested it and it worked fine. You could put the right click event handler on whatever controls are in the template.

    Wiring the event to the Listbox won't help, since it won't know which item you right clicked on.

    Friday, July 2, 2010 6:23 PM
  • Add this

     <ListBox.ItemContainerStyle>
                        <Style TargetType="ListBoxItem">
                            <Setter Property="HorizontalContentAlignment"
                                    Value="Stretch"></Setter>
                        </Style>
                    </ListBox.ItemContainerStyle>
    after this
    <ListBox x:Name="myListBox">
    Friday, July 2, 2010 7:10 PM
  • The last piece about adding the was key. Can you explain why this made it work?
    Saturday, July 3, 2010 12:30 AM
  • I still would have worked if you had right clicked onany of the controls that had text in them. However, the StackPanel that enclosed them wasn't stretched all the way across the ListBox. By setting the contaiiner style of HorizontalContentAlignment to Stretch, that made the StackPanel fill the entire horizontal distance. That made it clickable.

    Saturday, July 3, 2010 6:13 AM
  • If you want to see the difference, put a border around the StackPanel. And toggle that HorizontalContentAlignment code. You'll see it shrink and grow.

    Saturday, July 3, 2010 6:15 AM