Dbloom
Dbloom

Reputation: 1402

Scrollviewer and scroll direction (Vertical vs. Horizontal)

I would like a textblock that has vertical scrolling. I have the following XAML

        <ScrollViewer HorizontalAlignment="Left" Height="90" Margin="10,416,0,0" VerticalAlignment="Top" Width="463" VerticalScrollBarVisibility="Auto"  HorizontalScrollBarVisibility="Auto" CanContentScroll="True" >
        <TextBlock Name="txtConfigPath" Text="" >                
        </TextBlock>
    </ScrollViewer>

This produces a textblock that only scrolls horizontally. I've tried everything I can think of but this control only wants to scroll horizontally.

Upvotes: 0

Views: 6023

Answers (2)

horiatu
horiatu

Reputation: 392

Add a grid with a row definition of * to accommodate your ScrollViewer and Auto for other rows (as for Header and footer.) Add this code for the ScrollViewer:

<UserControl ...>

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>

    <TextBlock><Run Text="Some text"/></TextBlock>

    <ScrollViewer 
        Grid.Row="1" 
        CanContentScroll="True" 
        VerticalScrollBarVisibility="Auto" 
        HorizontalScrollBarVisibility="Disabled" 
        MinWidth="{Binding ActualWidth, 
            BindsDirectlyToSource=True, 
            ElementName=userControl, Mode=OneWay}">

Upvotes: 1

Chris W.
Chris W.

Reputation: 23280

You have several options here. You can set TextWrapping=Wrap on the TextBlock and Disable the HorizontalScrolling on the ScrollViewer, or you can set the TextWrapping on the TextBlock and either set a fixed width to your TextBlock or you can Bind it's MaxWidth to the ActualWidth of the ScrollViewer like TextBlock MaxWidth="{Binding RelativeSource={RelativeSource AncestorType=ScrollViewer}, Path=ActualWidth}"

Hope this helps, cheers!

Upvotes: 4

Related Questions