我正在 WPF 中使用文本框/richtextbox 组件,我需要将其他文本框导入其中。目前,我使用 RichTextbox 控件来插入额外的自定义文本框(它们是必要的,因为它是一个无法以其他方式完成的表达式)。我遇到的问题是,当光标与 richtexbox 内部的文本框相邻时,我需要将焦点集中在文本框中。它似乎忽略了该组件并跳过它。还有人有解决办法吗?
我似乎无法对 WPF 中的 RichTexbox 内的光标或组件进行任何控制。
最佳答案
嵌入在我的 RichTextBox 中的 UIComponent 实际上是一个数字,其中包含 3 个文本框(基数、上标和下标)。我遇到的问题是光标无法聚焦到数字部分。
我正在寻找的功能是这个。 。 .
RichTextBox.CaretPosition.GetAdjacentElement(LogicalDirection Direction)
这是我的代码。 。 .
public class MathRichTextbox : RichTextBox
{
public MathRichTextbox()
{
this.PreviewKeyDown += MathRichTextbox_PreviewKeyDown;
}
void MathRichTextbox_PreviewKeyDown(object sender, KeyEventArgs e)
{
Digit expr = null;
switch (e.Key)
{
case Key.Left:
expr = findAdjacentMathDigits(LogicalDirection.Backward);
break;
case Key.Right:
expr = findAdjacentMathDigits(LogicalDirection.Forward);
break;
}
if (expr != null)
this.Dispatcher.BeginInvoke(
new ThreadStart(() => expr.FocusBase()),
System.Windows.Threading.DispatcherPriority.Input, null);
}
private Digit findAdjacentMathDigits(LogicalDirection direction)
{
Digit expr = null;
if (Selection.Text.Length == 0)
{
DependencyObject dpObj = CaretPosition.GetAdjacentElement(
direction);
// is it contained in BlockUIContainer?
expr = CaretPosition.GetAdjacentElement(
direction) as Digit;
// is it onctained in a InlineUIContainer?
if (expr == null)
{
InlineUIContainer uiWrapper =
CaretPosition.GetAdjacentElement(
direction) as InlineUIContainer;
if (uiWrapper != null)
expr = uiWrapper.Child as Digit;
}
}
return expr;
}
}
关于c# - DotNet 中 Textbox/RichTextbox 内部的组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14027633/