![](/img/trans.png)
[英]When I click on a treelist, I want a word to appear in a textbox with devExpress
[英]Textbox pin to top when i click on it
我有一个简单的TextBox:
<TextBox Name="PART_txtBx" IsReadOnly="True" ScrollViewer.VerticalScrollBarVisibility="Auto" />
从代码背后我每次都添加一些文本:
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:我注意到创建文本框时,即使有焦点,也没有任何插入符号。 仅在执行单击时显示插入符号,但是单击后我无法在文本框中找到更改内容。 我的文本框是只读的,所以我不需要插入符号。
可能: PART_txtBx.CaretIndex =PART_txtBx.Length;
或: PART_txtBx.SelectionStart = PART_txtBx.Text.Length; PART_txtBx.ScrollToCaret();
PART_txtBx.SelectionStart = PART_txtBx.Text.Length; PART_txtBx.ScrollToCaret();
ScrollToEnd()。 与textbox_changed事件
为您的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; }
处理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 }
在手动输入文本的情况下,您将不得不选择文本
希望能帮助到你
因此,我创建了自己的CustomTextblock来解决此问题:
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;
}
}
}
当我手动将其滚动到底部时,此CustomTextBox还将滚动条固定在底部。 xaml:
<v:CustomTextBox
IsReadOnly="True"
ScrollViewer.HorizontalScrollBarVisibility="Auto"
ScrollViewer.VerticalScrollBarVisibility="Auto"
/>
遗憾的是,此实现选择被打破了。 我会调查一下
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.