简体   繁体   中英

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?

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>

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>

Source

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM