简体   繁体   中英

Horizontal scroll bar doesn't appear on TextBox

I have a Grid which has a TextBox inside a ScrollViewer:

    <Grid DockPanel.Dock="Top">
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>
        <ScrollViewer>
            <StackPanel Height="271" Width="258">
                <Label FontSize="15" Margin="10"> Suggestions </Label>
                <Expander x:Name="expander" Margin="10" />
            </StackPanel>
        </ScrollViewer>
        <GridSplitter Grid.Column="0" Width="5" />
        <ScrollViewer Grid.Column="1">
            <TextBox x:Name="textBox" AcceptsReturn="True"
                AcceptsTab="True" FontSize="15"
                VerticalScrollBarVisibility="Visible" 
                HorizontalScrollBarVisibility="Visible"
                TextWrapping="WrapWithOverflow" Language="en-US"
                SpellCheck.IsEnabled="True"/>
        </ScrollViewer>
    </Grid>

Even if I set HorizontalScrollBarVisibility to Visible, the horizontal scroll bar is not visible, and when I type some text that goes beyond the TextBox's width, I can't scroll:

在此处输入图片说明

Looking at the implementation of the TextBox: http://referencesource.microsoft.com/#PresentationFramework/src/Framework/System/Windows/Controls/TextBox.cs#1473

it seems that this is the normal behavior: the horizontal scrollbar is not visible when TextWrapping is WrapWithOverflow.

Based on that, the only possible way to show the horizontal scrollbar of the TextBox is to set the TextWrapping to NoWrap.

A workarount to what (I think that) you want to achieve with the outer ScrollViewer might be:

<ScrollViewer Grid.Column="1" HorizontalScrollBarVisibility="Visible">
    <TextBox x:Name="textBox" AcceptsReturn="True"
        AcceptsTab="True" FontSize="15"
        VerticalScrollBarVisibility="Hidden" 
        HorizontalScrollBarVisibility="Hidden"
        TextWrapping="WrapWithOverflow" Language="en-US"
        SpellCheck.IsEnabled="True"/>
</ScrollViewer>

It appears to me that the TextBox element is not being entirely displayed. I'd bet that the scroll bar is there, you just cannot see it without re-sizing your current window or scrolling downwards in the main window.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM