2016-08-16 37 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); 
    } 
} 

不要忘記在空載刪除處理。

相關問題