What can cause PreviewMouseWheel to not tunnel over the margins of a great-grandchild UI element?

Jon Butterfield 1 Reputation point
2020-09-02T15:14:27.63+00:00

I have a UserControl with a Border (CornerRadius="15" Margin="5,5,5,5"), that has many copies of itself placed inside a StackPanel inside a ScrollViewer inside a TabItem inside a TabControl. I am able to scroll inside my ScrollViewer because I steal the event from my UserControl and re-raise it. But I cannot scroll inside the spaces between my UserControls (the margins). In fact, when I try to scroll inside the margins, neither the UserControl, the StackPanel, the ScrollViewer, nor the TabItem receive the PreviewMouseWheel event. How can the PreviewMouseWheel be conditional, and only work over certain portions of the visible area?

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,783 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. DaisyTian-1203 11,626 Reputation points
    2020-09-03T02:34:25.397+00:00

    I will show you a demo to invoke the PreviewMouseWheel in the UserControl:
    The code for UserControl1.xaml:

    <UserControl x:Class="InvokePreviewMouseWheel.UserControl1"  
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
                 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"   
                 xmlns:d="http://schemas.microsoft.com/expression/blend/2008"   
                 xmlns:local="clr-namespace:InvokePreviewMouseWheel"  
                 mc:Ignorable="d"   
                 d:DesignHeight="450" d:DesignWidth="800"  
                 Name="myUserControl"  
                 PreviewMouseWheel="myPreviewMouseWheel"  
                 >  
    
        <Grid>  
            <Border CornerRadius="15" Margin="5,5,5,5" BorderThickness="2" Background="Azure" >  
                <TabControl Name="myTabControl" PreviewMouseWheel="myPreviewMouseWheel">  
                    <TabItem Name="myTabItem" PreviewMouseWheel="myPreviewMouseWheel">  
                        <TabItem.Header>  
                            <StackPanel Orientation="Horizontal">  
                                <Ellipse Width="10" Height="10" Fill="DarkGray"/>  
                                <TextBlock>Tab 1</TextBlock>  
                            </StackPanel>  
                        </TabItem.Header>  
                        <StackPanel Name="myStackPanel" PreviewMouseWheel="myPreviewMouseWheel">  
                            <ScrollViewer Name="myScrollViewer" HorizontalScrollBarVisibility="Auto" Height="200" PreviewMouseWheel="myPreviewMouseWheel">  
                                <StackPanel>  
                                    <ListBox x:Name="listBox" Height="250" Width="300" FontFamily="15" PreviewMouseWheel="myPreviewMouseWheel"></ListBox>  
                                </StackPanel>  
    
                            </ScrollViewer>  
                        </StackPanel>  
                    </TabItem>  
                    <TabItem Header="Tab 2">  
                        <TextBlock Text="{Binding ElementName=textBox1, Path=Text}"/>  
                    </TabItem>  
                </TabControl>  
            </Border>    
        </Grid>  
    </UserControl>  
    

    The code for UserControl1.xaml.cs:

     private void myPreviewMouseWheel(object sender, MouseWheelEventArgs e)  
            {  
                string message = "PreviewMouseWheelEvent_Element:" + (sender as FrameworkElement).Name.ToString();  
                this.listBox.Items.Add(message);           
            }  
    

    Then input the UserControl1 in the MainWindow.xaml : <local:UserControl1></local:UserControl1>

    Build and run the project and you will get the below result:
    22149-3.gif

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.