Consider first version of code (MainWindow.xaml):
<ScrollViewer>
<local:CustomControl1 Width="1000" Height="1000" Background="Red"/>
</ScrollViewer>
where CustomControl1 derived from ItemsControl. Inside CustomControl1 , I overriding OnMouseDown event. This code works perfectly, and i do catch mouse down event.
Now second version of code (MainWindow.xaml):
<local:CustomControl1 Width="1000" Height="1000" Background="Red"/>
Inside Generic.xaml, I changing template of my items control:
<ControlTemplate TargetType="{x:Type local:CustomControl1}">
<Border Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<ScrollViewer
VerticalScrollBarVisibility="Visible"
HorizontalScrollBarVisibility="Visible"
>
<ItemsPresenter/>
</ScrollViewer>
</Border>
</ControlTemplate>
When I putting scrollviewer as part of control template, I DO NOT receiving OnMouseDownEvent anymore (for left click). For some reason, now the mouse down event marked as handled. If instead of overriding OnMouseDown I using the following statement inside items control constructor, the event is catched:
AddHandler(Mouse.MouseDownEvent, new MouseButtonEventHandler(OnMouseDown), true);
First, I would like to understand why placing scrollviewer inside template changing behavior of mouse down. Second, does anyone know some workaround? The solution I proposed (by catching handled events) is not acceptable for me. In my application, I need to handle mouse down events only if none of items control children handled it.
Thanks in advance.
If you look at the default Template
for a ListBox
for example (which also derives from ItemsControl
), you'll see that the ScrollViewer
has Focusable="False"
set in the Template. This allows the mouse events to pass through to your control
<ScrollViewer VerticalScrollBarVisibility="Visible"
HorizontalScrollBarVisibility="Visible"
Focusable="False" >
<ItemsPresenter/>
</ScrollViewer>