【问题标题】:Components Inside Textbox/RichTextbox in DotNetDotNet 中 Textbox/RichTextbox 内部的组件
【发布时间】:2012-12-11 05:55:36
【问题描述】:

我正在使用 WPF 中的文本框/富文本框组件,我需要将其他文本框导入其中。目前,我使用一个 RichTextbox 控件,该控件将其他自定义文本框插入其中(它们是必要的,因为它是一个无法以其他方式完成的表达式)。我遇到的问题是当光标与richtexbox 内的文本框相邻时,我需要将注意力集中在文本框中。它似乎忽略了该组件并跳过它。其他人有解决这个问题的方法吗?

我似乎无法控制 WPF 中 RichTexbox 内的光标或组件。

【问题讨论】:

  • 哇。与溢出相比,这个地方已经死了。 . .还是因为是平安夜?
  • 您应该尝试使用Run。请参阅此link 和最终解决方案

标签: c# wpf richtextbox


【解决方案1】:

嵌入在我的 RichTextBox 中的 UIComponent 实际上是一个包含 3 个文本框(基数、上标和下标)的数字。我遇到的问题是光标无法聚焦到数字组件上。

我正在寻找的功能是 this 。 . .

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;
    }

}

【讨论】:

  • 不错;你不应该设置e.Handled = true?
  • 当然你可能会发现RichTextBox.SelectionCharOffset的财产很有趣。
  • 我可能会在未来,但我不想打扰用户,如果它没有找到任何东西,如果它找到了,它会专注于自己。感谢您的提醒。
  • 其实我现在还不知道如何使用该属性 (RichTextBox.SelectionCharOffset),但我一定会进一步研究。
  • 我还发现这个网站写了一个非常好的项目,写了关于标记文本,你可能感兴趣blog.pixelingene.com/2010/10/…
【解决方案2】:

正如我在评论中所说,您应该尝试使用Run

Run 与 TextBox 没有太大区别。举个例子吧:

您想在 RichTextBox 中 Paragraph 的开头添加此字符串“This is an example”:

Paragraph _para //I assume you have this
TextPointer pointer=_para.ContentStart;
Run run=new Run("This is an example",pointer);

就是这样。您可以设置 FontSize、FontFamily 和 ... 其他属性,例如 TextBox

run.Foregroung=Brushes.Red;

希望对你有帮助。

【讨论】:

  • 伙计,你的指针/链接真的很有帮助。我想我可能有一个有效的解决方案。如果可行,我会尽快与您分享。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-28
  • 1970-01-01
  • 2019-07-13
相关资源
最近更新 更多