c# - DotNet 中 Textbox/RichTextbox 内部的组件

标签 c# wpf richtextbox

我正在 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/

相关文章:

c# - 在 Visual Studio 中添加没有完整命名空间的监视

c# - 如何使用 Html.TextBoxFor 设置默认值?

c# - 用于创建向导 UI 的 WPF MVVM UserControl 绑定(bind)

c# - 在 RichTextBox 中查找与所选文本关联的 RTF 代码

c# - 使用嵌套母版页检查身份验证和授权的最佳方法是什么

c# - 使用 linq 检查列表计数是否一致

c# - Facebook,构建手动登录流程 - C# SDK

c# - 将嵌套字段绑定(bind)到 devexpress Grid 列

c# - 如何在 wpf 的 richtextbox 中禁用 ctrl+A 和 ctrl+c?

c# - 如何在用户书写时在 RichTextBox 中用不同的颜色为不同的单词着色,并在单击该彩色文本时引发事件