Mustafa Rezaei
Mustafa Rezaei

Reputation: 61

Scrollable Control in ScrollViewer WPF

I made a control for selecting Date. you can increase or decrease Day,Month or year just by Scrolling MouseWheel. but when I put this control in ScrollViewer MouseWheel will scroll ScrollViewer and MyControl not work properly. I have searched a lot to prevent ScrollViewer from handling MouseWheel but I couldn't.

this is my Xaml:

<ScrollViewer>
    <StackPanel>
         some other elements...

            <controls:DateTimeSelect Width="400" Value="{x:Static system:DateTime.Now}"/>

         some other elements...
    </StackPanel>
</ScrollViewer>

Upvotes: 0

Views: 387

Answers (2)

Mustafa Rezaei
Mustafa Rezaei

Reputation: 61

Thank for your answers. I tried so many ways but I realized that ScrollViewer not working as expected. so I made a new project and copied all files from old project to new one step by step and after each change I checked whether scrollerViewer woks properly or not. I am using DevExpress MVVM framework so I have to create and show mainWindow in Bootstarpper and remove StartupUri from App.xaml. I realized that this is where I have problem, every time I set StartupUri and and let App class to show MainWindow nothing is wrong but as I Creating MainWindow manually and show ScrollViewer catches MouseWheel and preventing its children to hadnling it.

Upvotes: 0

fussel
fussel

Reputation: 151

You can sign up for PreviewMouseWheel event fromScrollViewer and forward it to your DateTimeSelect control.

This code directs the PreviewMouseWheel event within the first ScrollViewer:

private void HandlePreviewMouseWheel(object sender, MouseWheelEventArgs e)
    {
        if (sender is ScrollViewer && !e.Handled)
        {
            e.Handled = true;
            var eventArg = new MouseWheelEventArgs(e.MouseDevice, e.Timestamp, e.Delta);
            eventArg.RoutedEvent = UIElement.MouseWheelEvent;
            eventArg.Source = sender;
            var parent = ((Control)sender).Parent as UIElement;
            parent.RaiseEvent(eventArg);
        }
    } 

For more information, see https://serialseb.com/blog/2007/09/03/wpf-tips-6-preventing-scrollviewer-from/.

To know more about how to use it in your case, further information about contol: DateTimeSelect is required.

Upvotes: 1

Related Questions