如何知道用户在RichEditBox
中点击键盘中的Enter
此代码不工作
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
中更改代码文本?
我想做一个编辑器,任何帮助都很感激。
对
看这个方法:
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";
}
你必须从Document中获取ITextRange。之后,您可以将其展开为TextRangeUnit。排队,得到整条线。现在您可以更改该行的样式和文本。
试试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);
}
}