.NET MAUI WebView VerticalScrollBar Windows

Ángel Rubén Valdeolmos Carmona 611 Reputation points
2023-07-28T09:34:02.8533333+00:00

how to hide the vertical scrollbar in the webview in windows?

Developer technologies | .NET | .NET MAUI
Developer technologies | C#
{count} votes

Accepted answer
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 50,126 Reputation points Microsoft External Staff
    2023-07-31T07:23:40.8566667+00:00

    Hello,

    After testing, scrollbars in Windows WebView are actually controlled internally by web pages, so you could inject JavaScript code into the webpage to hide it.

    Please refer to the following code:

    // in xaml
    <WebView x:Name="test_web" WidthRequest="500" HeightRequest="600" Source="https://www.microsoft.com/"></WebView>
    
    // in code-behind
    
    protected override void OnHandlerChanged()
        {
            base.OnHandlerChanged();
    #if WINDOWS
                var webview = test_web.Handler.PlatformView as WebView2;
            webview.CoreWebView2Initialized += (sender, args) =>
            {
                if (sender.CoreWebView2 != null)
                {
                    sender.CoreWebView2.DOMContentLoaded += (s, a) =>
                    {
                        sender.ExecuteScriptAsync("document.querySelector('body').style.overflow='scroll';var style=document.createElement('style');style.type='text/css';style.innerHTML='::-webkit-scrollbar{display:none}';document.getElementsByTagName('body')[0].appendChild(style)");
                    };
                }
            };
    #endif
        }
    

    Best Regards,

    Alec Liu.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Dejan Basic 0 Reputation points
    2025-02-14T19:26:57.0733333+00:00

    I just used InputTransparent="True" for WebView to disable scrolling on iOS.

    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.