WPF如何在TextBox中查看最后添加的文本行

时间:2010-05-10 12:14:36

标签: wpf-controls

我想在我的WPF应用中模拟控制台文本输出 但是当我在TextBox中添加新行时,我应该使用滚动条来查看最后添加的文本,但我想看到最后添加的文本但是对于第一行使用滚动条

<TextBox TextWrapping="Wrap" AcceptsReturn="True" VerticalScrollBarVisibility="Auto"
                   Text="{Binding Path=Data, Mode=TwoWay}" />`

3 个答案:

答案 0 :(得分:1)

在添加文本后使用TextBox的ScrollToLine方法(以及LineCount属性来了解有多少行),以确保刚刚添加的行可见。

答案 1 :(得分:1)

请考虑直接从后面的代码滚动文本框(例如,当文本更改时):

private void SampleTextBox_TextChanged(object sender, TextChangedEventArgs e)
{
   if (SampleTextBox.LineCount != -1)
   {
      SampleTextBox.ScrollToLine(SampleTextBox.LineCount - 1);
   }
}

请告诉我这是否有帮助。

答案 2 :(得分:0)

感谢您的回答:我希望从XAML中做到这一点但是因为我感到不适,所以只能从代码背后 所以现在我的意思是使用复选框停止ScrollToEnd函数:

public partial class MainWindow : Window
{
    private bool isScrollToEnd; 
    Timer timer; 

    public double WaitTime 
    {
        get { return waitTime / 1000; }
        set { waitTime = value * 1000; }
    }
    private double waitTime;

    public MainWindow()
    {
        InitializeComponent();
        isScrollToEnd = true;
        waitTime = 5000;
        tbWaitTime.DataContext = this;
        timer = new Timer(waitTime);
        timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
    }

    // событие изменения текста в контроле tbConsole
    private void tbConsole_TextChanged(object sender, TextChangedEventArgs e)
    {
        if (tbConsole.LineCount != -1 && isScrollToEnd)
        {
            tbConsole.ScrollToLine(tbConsole.LineCount - 1);
            cbIsScrolling.IsChecked = false;
        } 
    }

   private void cbIsScrolling_Click(object sender, RoutedEventArgs e)
    {
        if ((bool)cbIsScrolling.IsChecked)
        {
            isScrollToEnd = !(bool)cbIsScrolling.IsChecked;
            isScrollToEnd = false;
            timer.Interval = waitTime;
            timer.Start(); 
            return;
        }
        isScrollToEnd = true;
        timer.Stop();
        cbIsScrolling.IsChecked = false;
    }

    void timer_Elapsed(object sender, ElapsedEventArgs e)
    {
        timer.Stop();
        isScrollToEnd = true;
    }
}

这是XAML代码:

 <StackPanel  Grid.Column="1" Grid.Row="2" Grid.ColumnSpan="2" Grid.RowSpan="2">
        <StackPanel Orientation="Horizontal" HorizontalAlignment="Right" Margin="0,3,10,2" VerticalAlignment="Top">
            <Label Content="Stop autoscrolling for:" />
            <TextBox Name="tbWaitTime" Text="{Binding Path=WaitTime}"
                     MinWidth="25" MaxWidth="50" Margin="5,0,0,0" />
            <Label Content="sec."/>
            <CheckBox Name="cbIsScrolling"  
                      HorizontalAlignment="Right" VerticalAlignment="Center"
                      Click="cbIsScrolling_Click" />
        </StackPanel>
        <TextBox Name="tbConsole" 
                     Background="LightGoldenrodYellow" Padding="5" Height="100"
                     VerticalScrollBarVisibility="Auto"
                     TextWrapping="Wrap" 
                     AcceptsReturn="True" 
                     Text="{Binding Path=Data, Mode=TwoWay}" TextChanged="tbConsole_TextChanged" />
    </StackPanel>