在 WPF 中,如何区分列表框项目或滚动条中的鼠标点击

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

我有一个包含许多项目的 WPF 列表框,因此显示了一个滚动条。我想为用户点击项目而不是滚动条添加处理程序。我该怎么做?

可以将 ItemTemplate 添加到 ListBox。您在此添加的任何 WPF 控件都可以添加各种事件处理程序,包括鼠标单击和拖动

<ListBox>
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel>
                <Label MouseLeftButtonDown="<new event handler>" Content="My Clickable Item"/>
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

Source

您可以使用 ItemContainerStyle 为使用 EventSetter

的项目设置点击事件处理程序
<ListBox>
    <ListBox.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
            <EventSetter Event="Click" Handler="myHandler"/>
        </Style>
    </ListBox.ItemContainerStyle>
</ListBox>