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.
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 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.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With