1

I have this screen, the user can change the Height, and based on that I want my ScrollViewer to show the scrollbar if needed but it turns to be always the same size.

Note that only the second line of the Parent Grid changes size (*) and based on that size I want my ScrollViewer size, and based on the content of the grid inside the ScrollViewer (that is added dynamically through code) the scrollBar should show.

<Grid Style="{StaticResource PopupBody}">
    <Grid.RowDefinitions>
        <RowDefinition Height="50" />
        <RowDefinition Height="*" />
        <RowDefinition Height="50" />
    </Grid.RowDefinitions>

    <!-- Header Panel -->
    <StackPanel x:Name="PopupHeader"
                    Grid.Row="0">
        <Label x:Name="PopupTitle"
                Style="{StaticResource PopupTitle}"
                Content="Column Updatable Detail"/>
    </StackPanel>

    <!-- Body Panel -->
    <DockPanel x:Name="PopupBody"
                    Grid.Row="1"
                    Margin="10,10,10,0" Height="350" VerticalAlignment="Top">


        <StackPanel DockPanel.Dock="Top" Margin="{StaticResource MarginSmallHorizontal}">
            <Grid Margin="{StaticResource MarginSmallVertical}">
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                </Grid.RowDefinitions>

                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="50" />
                    <ColumnDefinition Width="250" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>

                (... Hidden to be more readable ... )

                <ScrollViewer Grid.Row="1" Grid.ColumnSpan="3"
                              VerticalScrollBarVisibility="Auto" 
                              CanContentScroll="True">
                    <Grid x:Name="gridData" 
                            ShowGridLines="True"
                            Margin="0,0,0,0" >
                    </Grid>
                </ScrollViewer>



            </Grid>
        </StackPanel>

    </DockPanel>

    <!-- Footer Panel -->
    <Border Grid.Row="2" 
                Style="{StaticResource FooterBorder}">
        <StackPanel x:Name="FooterPanel"
                        Style="{StaticResource FooterPanel}">

            <Button x:Name="CancelButton"
                    Content="Close"
                    Style="{StaticResource FooterSecondaryButton}"
                    Click="OnCancelClicked"/>

        </StackPanel>
    </Border>
</Grid>

Can anybody help with this?

4
  • Hint, Height="350" is not dynamic. Commented Jan 30, 2018 at 21:53
  • @Paparazzi even removing the Height, doesn't work properly Commented Jan 30, 2018 at 23:13
  • Code has lots of problems Commented Jan 31, 2018 at 7:44
  • @Paparazzi thanks for the constructive message Commented Jan 31, 2018 at 13:38

1 Answer 1

6

The quagmire of nested panels in your Body Panel section is to blame here. I've created a minimal skeleton showing what I think you want. Use this as a starting point:

<Window x:Class="WpfTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Width="200"
        Height="300">

  <!-- Layout Root -->
  <Grid>
    <Grid.RowDefinitions>
      <RowDefinition Height="Auto" />
      <RowDefinition Height="*" />
      <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>

    <!-- Header Panel -->
    <Border Grid.Row="0" Background="#CCCCCC" Padding="11">
      <!-- Replace this TextBlock with your header content. -->
      <TextBlock Text="Header Content" TextAlignment="Center" />
    </Border>

    <!-- Body Panel -->
    <Grid Grid.Row="1" Background="#CCCCFF">
      <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="*" />
      </Grid.RowDefinitions>

      <Border Grid.Row="0" Background="#FFCCCC" Padding="11">
        <!-- Replace this TextBlock with your upper body content. -->
        <TextBlock Text="Upper Body Content" TextAlignment="Center" />
      </Border>

      <ScrollViewer Grid.Row="1" Padding="11"
                    VerticalScrollBarVisibility="Auto">

        <!-- Replace this Border with your scrollable content. -->
        <Border MinHeight="200">
          <Border.Background>
            <RadialGradientBrush RadiusY="1" RadiusX="1" Center="0.5,0.5">
              <GradientStop Color="White" Offset="0" />
              <GradientStop Color="Black" Offset="1" />
            </RadialGradientBrush>
          </Border.Background>
        </Border>

      </ScrollViewer>
    </Grid>

    <!-- Footer Panel -->
    <Border Grid.Row="2" Background="#CCFFCC" Padding="11">
      <!-- Replace this TextBlock with your footer content. -->
      <TextBlock Text="Footer Content" TextAlignment="Center" />
    </Border>
  </Grid>

</Window>

The screenshot below shows how the layout responds to vertical size changes. Note how the vertical scrollbar becomes visible once the height passes below the size required to show the main body content.

Screenshot showing dynamic layout

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

6 Comments

Hey @Mike thanks for your answer, but I don't know if I put a Grid that keeps growing (as it is dynamically filled), when I put for example Height="300" for the scrollViewer, the scrollbar shows, but the height isn't dynamic so if the user change the height the screen look ungly
Why are you setting a height on the ScrollViewer? I thought you wanted it to grow to fill the available space?
Just telling as an example that when I set the height it works ... when I don’t set it never shows the scrollbar and doesn’t show also the lines and controls of my inside grid that are beyond the initial size of the screen
Okay, let's try this again. I revised my code snippet. Please run it verbatim; don't change anything other than the class name and namespace. See how it reacts when you resize the window. Is this the behavior you want? If so, start with my Xaml and then drop in your content one piece at a time, verifying that it still works. That way, if something breaks, you'll know what piece broke it.
I was doing this with the previous code hehehe, but looking to your example it seems to be nice ... I'l tell you. Appreciate your help!
|

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.