WPF Modal Window Disables Scrollbars (ScrollViewer)

TmeedEl Shalom 1 Reputation point
2022-02-05T22:15:25.597+00:00

The code below creates a working scrollbar if the Window host is non-modal using Show();

The code below creates a non-working scrollbar if the Window host is in Modal Mode using ShowDialog();

Incidentally even using the Xceed.Wpf.Toolkit.MessageBox instead of a normal WPF window results in creating a non-working scrollbar. So the issue seems to stem from the Modal Window state.

This post from almost a decade ago reports the same issue! Modal windows don't support scroll: wpf scrollviewer dialogbox.

I tried setting the ScrollViewer and StackPanel to the same height, that failed to make a difference. I tried compiling in 4.8 (vs 4.0) that also failed to make a difference. I'm open to other workarounds as well because this issue seems systemic. My intent is to show a Modal Window when an exception is raised, pausing the main thread in the application until the Modal Window exits.

    var myScrollViewer = new ScrollViewer();
    var Stacker = new StackPanel();

    myScrollViewer.HorizontalScrollBarVisibility = ScrollBarVisibility.Auto;
    myScrollViewer.Height = 360;
    Stacker.Height = 360;
    Stacker.Children.Add(Text_Title);
    Stacker.Children.Add(Paragraph_X);

    // Add the StackPanel as the lone child of the ScrollViewer
    myScrollViewer.Content = Stacker;
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,666 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Hui Liu-MSFT 37,946 Reputation points Microsoft Vendor
    2022-02-07T05:19:51.003+00:00

    You could implement it in Xaml. When the height of the StackPanel is greater than the height of the ScrollViewer, the ScrollBar works. If I misunderstood, please let me know.
    ModalWindow.xaml:

    <Window x:Class="SaveFileFromMultipleTabItems.Window1"  
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
            xmlns:local="clr-namespace:SaveFileFromMultipleTabItems" WindowStartupLocation="CenterScreen"  
            mc:Ignorable="d"  
            Title="Window1" Height="360" Width="400">  
        <ScrollViewer x:Name="myScrollViewer" Height="360">  
            <StackPanel x:Name="Stacker" Height="460">  
                <Button Click="Button_Click" Content="click"/>  
                <Button Click="Button_Click" Content="click"/>  
                <RichTextBox Width="200" Height="100"   FontSize="40">  
                    <FlowDocument>  
                        <Paragraph >  
                            <Run >Paragraph 1,Paragraph 1,Paragraph 1,Paragraph 1,Paragraph 1,Paragraph 1,Paragraph 1,Paragraph 1,Paragraph 1,Paragraph 1,Paragraph 1</Run>  
                        </Paragraph>  
                        <Paragraph >  
                            <Run >Paragraph 2,Paragraph 2,Paragraph 2,Paragraph 2,Paragraph 2</Run>  
                        </Paragraph>  
                    </FlowDocument>  
                </RichTextBox>  
                <RichTextBox Width="200" Height="100"   FontSize="40">  
                    <FlowDocument>  
                        <Paragraph >  
                            <Run >Paragraph 1,Paragraph 1,Paragraph 1,Paragraph 1,Paragraph 1,Paragraph 1,Paragraph 1,Paragraph 1,Paragraph 1,Paragraph 1,Paragraph 1</Run>  
                        </Paragraph>  
                        <Paragraph >  
                            <Run >Paragraph 2,Paragraph 2,Paragraph 2,Paragraph 2,Paragraph 2</Run>  
                        </Paragraph>  
                    </FlowDocument>  
                </RichTextBox>  
                <TextBox Width="200" Height="50" Text="hello" TextWrapping="Wrap" FontSize="40"/>  
                <TextBox Width="200" Height="50" Text="hello" TextWrapping="Wrap" FontSize="40"/>  
            </StackPanel>  
        </ScrollViewer>  
    </Window>  
    

    Modal.xmal.cs:

     public partial class Window1 : Window  
      {  
        public Window1()  
        {  
          InitializeComponent();  
        }  
        private void Button_Click(object sender, RoutedEventArgs e)  
        {  
          MessageBox.Show("hello");  
        }  
      }  
    

    MainWindow.xaml:

     <StackPanel >  
                <Button Content="open modal window" Height="40" Click="Button_Click"/>  
                <Button Content="click" Height="40" Click="Button_Click_1"/>  
            </StackPanel>  
    

    MainWindow.xaml.cs:

    public partial class MainWindow : Window  
      {  
        public MainWindow()  
        {  
          InitializeComponent();  
        }  
        private void Button_Click(object sender, RoutedEventArgs e)  
        {  
          Window1 window = new Window1();  
          window.ShowDialog();  
        }  
        private void Button_Click_1(object sender, RoutedEventArgs e)  
        {  
          MessageBox.Show("hello");  
        }  
      }  
    

    The result:
    177811-3.gif


    If the response is helpful, please click "Accept Answer" and upvote it.
    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.

    0 comments No comments