TextBox Scrollbar始终保持在开头

时间:2014-11-22 16:20:48

标签: c# wpf textbox scrollbar

我到处搜索,我找不到答案。如果我使用Microsoft's TextBox template(包含ScrollViewer),则滚动条就在我输入的位置。然而,当我向此ScrollViewer添加style时(即使Microsoft's),滚动条仍保留在滚动查看器的开头,即使我的光标离开TextBox也是如此。你可以向我解释所有这些背后的魔力,并提出一种方法让滚动条跟随光标吗?

以下是代码:

<!-- TexBox Style -->
<Style TargetType="{x:Type TextBox}"> 
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type TextBoxBase}">
                <Border Name="Border" CornerRadius="{StaticResource TextBoxBorderRadius}"
                    BorderThickness="1"
                    Background="{StaticResource TextBoxBackroundBrush}"
                    BorderBrush="{StaticResource TextBoxBorderBrush}">
                    <ScrollViewer Margin="0"
                                  Template="{StaticResource ScrollViewerTemplate}"
                                  x:Name="PART_ContentHost"/>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

<!-- ScrollViewer Template -->
<ControlTemplate x:Key="ScrollViewerTemplate" TargetType="{x:Type ScrollViewer}">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition Width="Auto"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>

        <ScrollContentPresenter Grid.Column="0"/>

        <ScrollBar Name="PART_HorizontalScrollBar"
                                Orientation="Horizontal"
                                Grid.Row="1"
                                Value="{TemplateBinding HorizontalOffset}"
                                Maximum="{TemplateBinding ScrollableWidth}"
                                ViewportSize="{TemplateBinding ViewportWidth}"
                                Visibility="{TemplateBinding ComputedHorizontalScrollBarVisibility}"/>
        <ScrollBar Name="PART_VerticalScrollBar"
                                Value="{TemplateBinding VerticalOffset}"
                                Grid.Column="1"
                                Grid.Row="0"
                                Maximum="{TemplateBinding ScrollableHeight}"
                                ViewportSize="{TemplateBinding ViewportHeight}"
                                Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}"/>
    </Grid>
</ControlTemplate>

0 个答案:

没有答案