【问题标题】:In WPF how do I distinguish a mouse hit in a list box item or the scroll bar在 WPF 中,如何区分列表框项或滚动条中的鼠标点击
【发布时间】:2017-08-04 00:05:10
【问题描述】:

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

【问题讨论】:

标签: c# wpf hittest


【解决方案1】:

您可以使用ItemContainerStyle 为使用EventSetter 的项目上的单击事件设置处理程序

<ListBox>
    <ListBox.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
            <EventSetter Event="Click" Handler="myHandler"/>
        </Style>
    </ListBox.ItemContainerStyle>
</ListBox>

【讨论】:

    【解决方案2】:

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

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

    Source

    【讨论】:

      猜你喜欢
      • 2018-06-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-10
      • 2011-08-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多