0

In our .NET MAUI application, we aim to determine if the Microsoft.UI.Xaml.Controls.Primitives.ScrollBar was pressed in WinUI platform. We've attempted to retrieve it by wiring PointerPressed and iterating through the parent of the OriginalSource from PointerRoutedEventArgs until encountering the scrollbar. Also Move action can be detected in content touch, while scrolling using scrollbar. Are there alternative approaches to obtain the scrollbar instance, in platform specific (WinUI) or .NET Maui?

   // PointerPressed event.
   private void PointerPressed(object sender, PointerRoutedEventArgs e)
   {
       if ((e.OriginalSource is Microsoft.UI.Xaml.Shapes.Rectangle || e.OriginalSource is Microsoft.UI.Xaml.Controls.Grid))
       {
           if (this.IsScrollBarPressed(e.OriginalSource))
           {
               return;
           }
       }
   }


   private bool IsScrollBarPressed(object touchElement)
   {
       DependencyObject parent = VisualTreeHelper.GetParent(touchElement as DependencyObject);
       while (parent != null)
       {
           parent = VisualTreeHelper.GetParent(parent);
           if (parent is ScrollBar)
           {
               return true;
           }
       }

       return false;

   }

1
  • It seems there is no better way to get the Microsoft.UI.Xaml.Controls.Primitives.ScrollBar. Maui doesn't implement the crossplatform view for it and the scrollbar is always used in the scrollviewer and scrollview. Commented Mar 19, 2024 at 5:06

1 Answer 1

0

There is one (1) way that might work, as it works for just about all seemingly "hidden" controls. What you do is find the style for the Scrollbar in the Generic.xaml file and then add that style to your resources and make sure that that style is going to be applied to the ScrollViewer's scrollbar through various methods that are available for achieving this. And now when OnApplyingTemplate() is called you can traverse the template tree and find the scrollviewer and even the scrollbar. Create a reference to the Scrollbar within your class so that you can use it as needed. Bingo.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.