Search code examples
wpfxamlscrollviewerstackpanel

WPF ScrollViewer for control


I have an issue with ScrollViewer. Here is the code:

<Grid VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
    <Grid.RowDefinitions>
        <RowDefinition Height="*"></RowDefinition>
        <RowDefinition Height="70"></RowDefinition>
        <RowDefinition Height="*"></RowDefinition>
    </Grid.RowDefinitions>

    <StackPanel  Orientation="Vertical" Grid.Row="0">
        <TextBlock x:Name="tbFrom" Text="MailSendTo=Send to:" HorizontalAlignment="Left" Margin="0,0,0,3"/>
        <ScrollViewer VerticalScrollBarVisibility="Auto">
            <util:ctlStaffPicker x:Name="ctlStaffPicker1" ForMessaging="True" TabIndex="0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"  MaxHeight="75"></util:ctlStaffPicker>
        </ScrollViewer>
    </StackPanel>

    <StackPanel Grid.Row="1" Margin="0,10,0,5" Height="70">
        <TextBlock HorizontalAlignment="Left" x:Name="tbQuickMessage" Text="LBLQuickMessage=Quick Message:" TextWrapping="Wrap" Margin="0,0,0,3"/>
        <TextBox x:Name="txtQuickMessage"
                MaxHeight="35"
                HorizontalAlignment="Stretch" 
                Text="{Binding MessageText, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:ctlNewMessage}}}"
                TextWrapping="Wrap"
                ScrollViewer.VerticalScrollBarVisibility="Auto" 
                TabIndex="1" />
    </StackPanel>

I know that a ScrollViewer inside StackPanel will never work fine. I want to make scrollable just <util:ctlStaffPicker>. Could you please help with some suggestions? Thanks in advance.

Here is small screenshot of the different scroller appearances, the second one is the classic one: different scroller appearances


Solution

  • Just use an internal Grid Panel instead:

    <Grid VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
        <Grid.RowDefinitions>
            <RowDefinition Height="*"></RowDefinition>
            <RowDefinition Height="70"></RowDefinition>
            <RowDefinition Height="*"></RowDefinition>
        </Grid.RowDefinitions>
    
        <Grid Orientation="Vertical" Grid.Row="0">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"></RowDefinition>
                <RowDefinition Height="*"></RowDefinition>
            </Grid.RowDefinitions>
            <TextBlock x:Name="tbFrom" Text="MailSendTo=Send to:" HorizontalAlignment="Left" Margin="0,0,0,3"/>
            <ScrollViewer Grid.Row="1" VerticalScrollBarVisibility="Auto" VerticalAlignment="Stretch">
                <util:ctlStaffPicker x:Name="ctlStaffPicker1" ForMessaging="True" TabIndex="0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"  MaxHeight="75"></util:ctlStaffPicker>
            </ScrollViewer>
        </Grid>
        <StackPanel Grid.Row="1" Margin="0,10,0,5" Height="70">
            <TextBlock HorizontalAlignment="Left" x:Name="tbQuickMessage" Text="LBLQuickMessage=Quick Message:" TextWrapping="Wrap" Margin="0,0,0,3"/>
            <TextBox x:Name="txtQuickMessage"
                    MaxHeight="35"
                    HorizontalAlignment="Stretch" 
                    Text="{Binding MessageText, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:ctlNewMessage}}}"
                    TextWrapping="Wrap"
                    ScrollViewer.VerticalScrollBarVisibility="Auto" 
                    TabIndex="1" />
        </StackPanel>
    </Grid>
    

    Please note that StackPanels should only be used for the simplest layout duties.