Question

I want to delete my listbox item on right click. But, right click event not worked in my case. Below is the code which I tried.

In constructor:

listBox1.MouseDown += new MouseButtonEventHandler(listBox1_MouseRightClick);

Right Click:

private void listBox1_MouseRightClick(object sender, MouseButtonEventArgs e)
    {
        if (sender is ListBoxItem)
        {
            ListBoxItem item = (ListBoxItem)sender;
            Harvest_TimeSheetEntry entryToDelete = (Harvest_TimeSheetEntry)item.DataContext;

            MessageBoxResult Result = System.Windows.MessageBox.Show("Are you sure?", "Delete Confirmation", System.Windows.MessageBoxButton.YesNo);

            if (Result == MessageBoxResult.Yes)
            {
                Globals._globalController.harvestManager.deleteHarvestEntry(entryToDelete);
            }
            else
            {
                System.Windows.MessageBox.Show("Delete operation Terminated");
            }
        }
    }

In xaml:

<ListBox x:Name="listBox1" ItemsSource="{Binding}" Margin="0,131,0,59" ItemTemplateSelector="{StaticResource templateSelector}" SelectionMode="Single" MouseRightButtonDown="listBox1_MouseRightClick">
    <ListBox.ItemContainerStyle>
       <Style TargetType="{x:Type ListBoxItem}">
           <EventSetter Event="MouseDown" Handler="listBox1_MouseRightClick"/>
       </Style>
    </ListBox.ItemContainerStyle>
</ListBox>

Please suggest, how should I use right click event so it could work?

Was it helpful?

Solution

Your original code seems redundant and verbose. MouseRightButtonDown isn't working because there is already an event handling listbox item selection and the ListBoxItem datacontext is simply the SelectedItem of listBox1.

Get rid of overriding the style and just declare the listbox with the preview event. This will tunnel MouseRightButtonDown instead of bubble it.

<ListBox x:Name="listBox1"
         ItemsSource="{Binding}"
         ItemTemplateSelector="{StaticResource templateSelector}"
         Margin="0,131,0,59"
         SelectionMode="Single" 
         PreviewMouseRightButtonDown="listBox1_MouseRightClick" />

In the constructor, get rid of this

listBox1.MouseDown += new MouseButtonEventHandler(listBox1_MouseRightClick);

Now in the event handler, sender is your listbox1 but if you're not tying this event to other listboxes, simply get the selectedItem from listbox1 and cast it to the appropriate object. Otherwise if you decide you want the functionality on multiple listboxes cast sender to ListBox

private void listBox1_MouseRightClick(object sender, MouseButtonEventArgs e)
{
      Harvest_TimeSheetEntry entryToDelete = (Harvest_TimeSheetEntry)listBox1.SelectedItem;
      if(entryToDelete != null)
      {
          //Do work
      }
}

OTHER TIPS

Deleting records on right click is not a good design and it leads users make more confuse the functionality. Still if you want to do something, then you can go for the PreviewMouseRightButtonDown event. Please see the below snippet

ListBox1.PreviewMouseRightButtonDown += new MouseButtonEventHandler(ListBox1_MouseRightButtonDown);

Change your XAML as follows

<ListBox x:Name="listBox1"
             ItemsSource="{Binding}"
             Margin="0,131,0,59"
             ItemTemplateSelector="{StaticResource templateSelector}"
             SelectionMode="Single">
        <ListBox.ItemContainerStyle>
            <Style TargetType="{x:Type ListBoxItem}">
                <EventSetter Event="PreviewMouseRightButtonDown"
                             Handler="ListBox1_PreviewMouseRightButtonDown" />
            </Style>
        </ListBox.ItemContainerStyle>
</ListBox>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top