RichEditBox如何知道光标所在的行

时间:2016-08-16 17:03:13

标签: win-universal-app uwp windows-10-universal richeditbox

我怎么知道如果用户点击Enter中的键盘中的RichEditBox

此代码不起作用

  private void Editor_KeyDown(object sender, KeyRoutedEventArgs e)
  {
      var dia = new MessageDialog(e.Key + "");
      dia.ShowAsync();
  }

此代码也不起作用

 private void Editor_KeyDown(object sender, KeyRoutedEventArgs e)
 {
     if (e.OriginalKey == (VirtualKey)(char)13)
     {
         NumberEditor.Text += Convert.ToString(_LineNum) + Environment.NewLine;
         ++_LineNum;
     }
 }

如何在RichEditBox中获取行,如何更改RichEditBox中代码的文字?

我想成为一名编辑,感谢任何帮助。

此致

1 个答案:

答案 0 :(得分:0)

看看这个方法:

   private void ChangeLine()
    {
        var textRange = MyRichEditBox.Document.GetRange(MyRichEditBox.Document.Selection.StartPosition, MyRichEditBox.Document.Selection.StartPosition);
        textRange.Expand(TextRangeUnit.Line);

        //Change line size.
        textRange.CharacterFormat.Size = 30;

        //Center the paragraph
        textRange.ParagraphFormat.Alignment = ParagraphAlignment.Center;

        //this will change text of the range
        textRange.Text = "My new text";
    }

从文档中你必须得到ITextRange。之后,您可以将其扩展为TextRangeUnit.Line并获得整行。现在您可以更改该行的样式和文本。

为KeyDownEvent尝试此代码:

 public sealed partial class TextEditPage : Page
{
    private readonly KeyEventHandler _keyDownHandler;

    //Constructor
    public TextEditPage()
    {
        this.InitializeComponent();
        this.Unloaded += OnUnloaded;

        //Add keydown event
        this._keyDownHandler = OnKeyDown;
        RtfBox.AddHandler(KeyDownEvent, this._keyDownHandler, true);
    }

    private void OnKeyDown(KeyRoutedEventArgs e)
    {
        //enter your code here
    }

    private void OnUnloaded(object sender, RoutedEventArgs routedEventArgs)
    {
        this.RemoveHandler(KeyDownEvent, _keyDownHandler);
    }
}

不要忘记在Unloaded中删除处理程序。