C# wpf - There are blue, yellow, and red cities in the ListBoxes (Pandemic Game). This is the red ListBox xaml:
A MouseDoubleClick jumps to the correct code in xaml.cs.
I've added MouseDown="gLBxOriginalRedCities_MouseDown".
When I click a city in "gLBxOriginalRedCities" the code does not jump to
private void gLBxOriginalRedCities_MouseDown(object sender, MouseButtonEventArgs e)
{
gLBxOriginalBlueCities.UnselectAll();
gLBxOriginalYellowCities.UnselectAll();
}
How do I detect a ListBox mouse click, and then unselect the other ListBox items?
Naimish MakwanaPosted Mar 26, 2024, 5:01 AM
The
MouseDownevent might not be the best choice for this scenario because theListBoxcontrol uses this event internally to fire itsSelectionChangedevent1. Therefore, yourMouseDownevent handler might not be called.Instead, you can use the
PreviewMouseLeftButtonDownevent1. This event is a tunneling event, which means it starts at the root of the visual tree and works its way down to the control that generated the event1. Since your code would get the chance to handle the event prior to theListBoxItem, this will get fired1.Here’s how you can modify your XAML:
And your event handler in C#:
This code will unselect all items in the other
ListBoxcontrols when you click an item ingLBxOriginalRedCities1.Remember to replace the variables with your own values. Also, ensure that the new driver is properly included in your project1.
Thanks