I have a WPF list box that contains many items so a scroll bar is shown. I want to add even handler for the user hitting the items but not the scroll bar. How can I do this?
Asked
Active
Viewed 105 times
1
-
Not quite a duplicate but similar: https://stackoverflow.com/questions/10733581/scrolling-while-dragging-and-dropping-wpf I ran into this with drag and drop; can't find the answer I used atm – BradleyDotNET Aug 04 '17 at 00:36
2 Answers
3
You can use the ItemContainerStyle
to set a handler for the click event on the items using EventSetter
<ListBox>
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<EventSetter Event="Click" Handler="myHandler"/>
</Style>
</ListBox.ItemContainerStyle>
</ListBox>

Dave M
- 2,863
- 1
- 22
- 17
2
An ItemTemplate
can be added to a ListBox
. Any WPF Controls you add in this can have various event handlers added to them, including mouse clicks and drags
<ListBox>
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<Label MouseLeftButtonDown="<new event handler>" Content="My Clickable Item"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>

Jonathan Tyson
- 463
- 4
- 9