Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When using ScrollViewer as part of template for some control, the left click is handled

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.

like image 705
Illidan Avatar asked Aug 14 '11 04:08

Illidan


People also ask

What is a ScrollViewer C#?

The ScrollViewer control encapsulates horizontal and vertical ScrollBar elements and a content container (such as a Panel element) in order to display other visible elements in a scrollable area. You must build a custom object in order to use the ScrollBar element for content scrolling.

How do I add a ScrollBar in WPF?

How to enable scrollbar in a WPF TextBox. The simplest way to add scrolling functionality to a TextBox control is by enabling its horizontal and vertical scrolling. The HorizontalScrollBarVisibility and VerticalScrollBarVisibility properties are used to set horizontal and vertical scroll bars of a TextBox.


1 Answers

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>
like image 149
Fredrik Hedblad Avatar answered Nov 03 '22 09:11

Fredrik Hedblad