WPF?不知道您是否发错了论坛,如果是的话,我可以帮你移到WPF论坛中去。

我想了下在WPF中你可以尝试如下方法:

新建一个继承ListBoxItem的类比如说ClickableListBoxItem,定义上Click事件,同样为了使用ClickableListBoxItem,一个继承ListBox的类也是需要的。在使用的时候给ClickableListBoxItem写上对应的Click处理代码即可。

或者还有一种更简便的方法如下:

<ListBox>
    <ListBox.ItemContainerStyle>
        <Style TargetType="{x:Type ListBoxItem}" BasedOn="{StaticResource {x:Type ListBoxItem}}">
            <EventSetter Event="MouseClick" Handler="ListBoxItem_MouseClick"/>
        </Style>
    </ListBox.ItemContainerStyle>
</ListBox>

--James

<THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
Thanks
MSDN Community Support
Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

WPF?不知道您是否发错了论坛,如果是的话,我可以帮你移到WPF论坛中去。

我想了下在WPF中你可以尝试如下方法:

新建一个继承ListBoxItem的类比如说ClickableListBoxItem,定义上Click事件,同样为了使用ClickableListBoxItem,一个继承ListBox的类也是需要的。在使用的时候给ClickableListBoxItem写上对应的Click处理代码即可。

或者还有一种更简便的方法如下:

<ListBox>
    <ListBox.ItemContainerStyle>
        <Style TargetType="{x:Type ListBoxItem}" BasedOn="{StaticResource {x:Type ListBoxItem}}">
            <EventSetter Event="MouseClick" Handler="ListBoxItem_MouseClick"/>
        </Style>
    </ListBox.ItemContainerStyle>
</ListBox>

--James

<THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
Thanks
MSDN Community Support
Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

<ListBox> <ListBox.ItemContainerStyle> <Style TargetType="{x:Type ListBoxItem}" BasedOn="{StaticResource {x:Type ListBoxItem}}"> <EventSetter Event="MouseClick" Handler="ListBoxItem_MouseClick"/> </Style> </ListBox.ItemContainerStyle> </ListBox>

后,点击item没有收到ListBoxItem_MouseClick事件处理,事件是否被截取了?

可能在EventSetter中的Event不应该是MouseClick,貌似MouseClick事件不是WPF的,WPF对应的是Click,你再试试看呢。

<THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
Thanks
MSDN Community Support
Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

可能在EventSetter中的Event不应该是MouseClick,貌似MouseClick事件不是WPF的,WPF对应的是Click,你再试试看呢。

<THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
Thanks
MSDN Community Support
Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

我没有使用MouseClick,因为wpf没有这个,我使用的是MouseDown,没有触发
<Setter Property="ItemContainerStyle">
<Setter.Value>
<Style TargetType="ListBoxItem">
<EventSetter Event="MouseDown" Handler="ListBoxItem_MouseClick"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<Border>
<ContentPresenter/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Setter.Value>
</Setter>