tags:

views:

116

answers:

2

Hi,

My DataTemplate looks like this:

<DataTemplate x:Key="ItemTemplate">
<StackPanel MouseLeftButtonDown="StackPanel_MouseLeftButtonDown">

However I can't catch mouse left button down. If I set the background of the template to some color, it is ok.

How do I create a transparent DataTemplate and catch the MouseLefButtonDown event?

Thank you.

A: 

MouseLeftButtonDown is a bubbling event. That means it fires first at the deepest possible level, then "bubbles" upward through the logical tree. If you have content contained in your StackPanel that is handling MouseLeftButtonDown, then you will never see the event bubble upward, and thus it will never reach your StackPanel.

PreviewMouseLeftButtonDown, on the other hand, is a tunneling event. That means that it will fire first at the top-level container, and then "tunnel" downwards through the logical tree until it reaches the lowest level. Try changing your event to:

<StackPanel PreviewMouseLeftButtonDown="StackPanel_PreviewMouseLeftButtonDown">

And see if you can catch it then.

Charlie
+1  A: 

If you would like to catch mouse events on the StackPanel itself you just have to set its background brush to be transparent:

<StackPanel MouseLeftButtonDown="StackPanel_MouseLeftButtonDown"
        Background="Transparent" Height="400" Width="400" >
<Button Content="dfsdf"/>

Milan Nankov
Well this is my problem. If background is Transparetn, I can't receive event. If it is set to Black, White etc I can receive event MouseLeftButtonDown.To better explain, I need to have list box item with transaprent background I'm able to drag by mouse. Therefore I need to receive MouseLeftButtonDown and start drag operation.
Dusan Kocurek