当我点击它时,文本框引脚顶部

时间:2015-06-25 09:36:48

标签: c# wpf textbox

我有一个简单的TextBox:

<TextBox Name="PART_txtBx" IsReadOnly="True" ScrollViewer.VerticalScrollBarVisibility="Auto"  />

从codebehind我每次都添加一些文字:

public MainWindow()
{
    InitializeComponent();

    DispatcherTimer dt = new DispatcherTimer();
    dt.Interval = TimeSpan.FromMilliseconds(100);
    dt.Tick += dt_Tick;
    dt.Start();
}

void dt_Tick(object sender, EventArgs e)
{
    PART_txtBx.Text += "hi\n";
}

当我点击文本框时,当我向其添加一些文字时,它会自动将滚动条置于顶部。

如果我像这样处理PreviewMouseLeftButtonDown事件:

private void PART_txtBx_PreviewMouseLeftButtonDown_1(object sender, MouseButtonEventArgs e)
{
    e.Handled = true;
}

文本框工作正常但我不能(当然)选择任何文本。

任何阻止此行为的想法?

编辑1: 我注意到在创建文本框时,即使它已经有焦点,也没有任何插入符号。插入符号仅在执行单击时显示,但在单击后无法找到文本框内的更改内容。 我的TextBox是只读的,因此我不需要插入符号。

3 个答案:

答案 0 :(得分:0)

5月:PART_txtBx.CaretIndex =PART_txtBx.Length;

或: PART_txtBx.SelectionStart = PART_txtBx.Text.Length; PART_txtBx.ScrollToCaret();

ScrollToEnd(). 使用textbox_changed事件

答案 1 :(得分:0)

  1. 您需要扩展TextBox控件以添加PreviewTextChanged事件,就像描述here一样。在PreviewTextChanged上,您必须记住SelectionStart和SelectionLength
  2. 处理TextBox的事件ScrollViewer.ScrollChanged:

    ScrollViewer.ScrollChanged="OnScrollChanged" <!--in xaml-->
    
    
    private void OnScrollChanged(object sender, ScrollChangedEventArgs e)
    {
        // here you could prevent scrolling
        isOnBottom = (sender as TextBox).VerticalOffset + (sender as TextBox).ViewportHeight == (sender as TextBox).ExtentHeight;
    }
    
  3. 处理TextChanged事件:

    TextChanged="OnTextChanged"
    
    private void OnTextChanged(object sender, TextChangedEventArgs e)
    {
        if (isOnBottom) (sender as TextBox).ScrollToEnd();
        // and here you can select text that was selected earlier
        // by using remembered SelectionStart and SelectionLength
    }
    
  4. 如果输入手动文本,则必须阻止文本选择

  5. 希望,这有帮助

答案 2 :(得分:0)

So i created my own CustomTextblock for resolving this issue:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace CE.EthernetMessagesManager.Views
{
    public class CustomTextBox : TextBox
    {
        private int realCaretIndex = 0;
        private bool triggeredByUser = false;
        private bool isScrollingWithMouse = false;

        public CustomTextBox()
            : base()
        {
            this.PreviewMouseLeftButtonDown += CustomTextBox_PreviewMouseLeftButtonDown;
            this.PreviewMouseLeftButtonUp += CustomTextBox_PreviewMouseLeftButtonUp;
            this.PreviewMouseMove += CustomTextBox_PreviewMouseMove;
            this.PreviewMouseWheel += CustomTextBox_PreviewMouseWheel;
            this.TextChanged += CustomTextBox_TextChanged;
            this.LostFocus += CustomTextBox_LostFocus;

            this.AddHandler(ScrollViewer.ScrollChangedEvent, new RoutedEventHandler((X, S) =>
            {
                if ((S as ScrollChangedEventArgs).VerticalChange != 0 && triggeredByUser)
                {
                    TextBox textBox = (X as TextBox);
                    int newLinePosition = 0;
                    newLinePosition = (int)(((S as ScrollChangedEventArgs).VerticalChange + textBox.ExtentHeight) / (textBox.FontSize + 2));

                    realCaretIndex += newLinePosition * ((S as ScrollChangedEventArgs).VerticalChange < 0 ? -1 : 1);
                    if (realCaretIndex < 0)
                        realCaretIndex = 0;

                    textBox.CaretIndex = realCaretIndex;
                    triggeredByUser = false;
                }
            }));
        }

        void CustomTextBox_LostFocus(object sender, System.Windows.RoutedEventArgs e)
        {
            e.Handled = true;
        }

        void CustomTextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            TextBox textBox = sender as TextBox;
            textBox.CaretIndex = realCaretIndex;

            var max = (textBox.ExtentHeight - textBox.ViewportHeight);
            var offset = textBox.VerticalOffset;

            if (max != 0 && max == offset)
                this.Dispatcher.Invoke(new Action(() =>
                {
                    textBox.ScrollToEnd();
                }),
                    System.Windows.Threading.DispatcherPriority.Loaded);
        }

        void CustomTextBox_PreviewMouseWheel(object sender, System.Windows.Input.MouseWheelEventArgs e)
        {
            triggeredByUser = true;
        }

        void CustomTextBox_PreviewMouseMove(object sender, System.Windows.Input.MouseEventArgs e)
        {
            if (isScrollingWithMouse)
            {
                TextBox textBox = sender as TextBox;
                realCaretIndex = textBox.GetCharacterIndexFromPoint(Mouse.GetPosition(textBox), true);
            }
        }

        void CustomTextBox_PreviewMouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            isScrollingWithMouse = false;
        }

        void CustomTextBox_PreviewMouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            TextBox textBox = sender as TextBox;
            realCaretIndex = textBox.GetCharacterIndexFromPoint(Mouse.GetPosition(textBox), true);
            triggeredByUser = true;
            isScrollingWithMouse = true;
        }
    }
}

this CustomTextBox also pin the scrollbar to the bottom when i manually put it to bottom. the xaml:

            <v:CustomTextBox 
                IsReadOnly="True"
                ScrollViewer.HorizontalScrollBarVisibility="Auto"
                ScrollViewer.VerticalScrollBarVisibility="Auto"
                />

Sadly with this implementation selection is broken. I'll look into it