开发者

Silverlight Listbox firing MouseRightButtonDown, but not MouseLeftButtonDown

I have this problem in a bigger Project...... so I set up a 'Testpoject' as Proof of Concept:

  • New Silverlight-Application
  • Add Listbox
  • Fill listbox with a few Checkboxes
  • Register listBox1_MouseLeftButtonDown
  • register listBox1_MouseRightButtonDown

You will see, that the listBox1_MouseLeftButtonDown won't fire under any circumstances.... listBox1_MouseRightButtonDown however fires just fine.

I tried using a custom Class deriving from ListBox and overriding, assuming something in the ListBox Class was setting e.Handled = false, but this did not change the behaviour, either.

Any Ideas on why this happens and how to fix?

(This problem also stops the 'parent'-control from recei开发者_开发百科ving the Click-Event... so the Event-passing is broke)

:edit: I fixed my problem with a workaround... so an answer is not required anymore. Just if somebody feels like figuring out why this is happening for the sake of it ;)


This seems to answer your question. To quote:

That's because ListBoxItem internally handles this event as well as the MouseLeftButtonDown event (halting the bubbling) to implement item selection.

The solution is to add the event handler in the code-behind file. From the article:

Although setting the RoutedEventArgs parameter's Handled property to true in a routed event handler appears to stop the tunneling or bubbling, individual handlers further up or down the tree can opt to receive the events anyway! This can only be done from procedural code, using an overload of AddHandler that adds a Boolean handledEventsToo parameter.

See the caveat at the end though.


This is by design. If you check the framework code, you'll see that the ListBoxItem sets the Handled property to true.

I had this same exact problem, so in my ListBoxItem.ItemTemplate, I added the event handler in my content.

<ListBox.ItemTemplate>
    <DataTemplate>
        <StackPanel Orientation="Horizontal" MouseLeftButtonDown="StackPanel_MouseLeftButtonDown">
        ... other controls ...
        </StackPanel>
    </DataTemplate>
</ListBox.ItemTemplate>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜