2016-08-16 56 views
1

我怎么能知道如果在键盘RichEditBox用户点击EnterRichEditBox如何知道哪一行光标在

此代码不起作用

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更改文本?

我想打一个编辑器,任何帮助表示赞赏。

问候

+0

对此有任何答案? – Noorul

回答

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

不要忘记在空载删除处理。