I have a ListBox with an ItemTemplate that contains a control that interacts with the mouse. This is due to the functionality of the ListBox selection, i.e. Clicking a control does not select the control. This is because the ListBoxItem sets the Handled property of the mouse event to true in OnMouseLeftButtonDown. I tried the following
protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e) { base.OnMouseLeftButtonDown(e); e.Handled = false; }
but ListBoxItem "takes over" the mouse and does not allow you to control your own interaction. Then I had another idea
protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e) { base.OnMouseLeftButtonDown(e); ((ListBoxItem)VisualTreeHelper.GetParent(VisualTreeHelper.GetParent(VisualTreeHelper.GetParent(this)))).IsSelected = true; }
which actually works, but feels more like an ugly kludge than an elegant solution. Are there any better solutions that don't depend on the exact contents of the visual tree?
source share