Search code examples
c#wpfhittest

In WPF how do I distinguish a mouse hit in a list box item or the scroll bar


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?


Solution

  • 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>