TextBox : 修改用户的输入
TextBox : Modify user's input
当用户添加一个;
时,我想在TextBox中添加; + Environment.NewLine
。
我找到了这个解决方案:
private void TextBox_OnPreviewTextInput(object sender, TextCompositionEventArgs e)
{
if (e.Text == ";")
{
e.Handled = true;
TextCompositionManager.StartComposition(
new TextComposition(InputManager.Current,
(IInputElement)sender,
";" + Environment.NewLine)
);
}
}
但是在这之后,撤消就不起作用了。
你能解释一下如何控制用户输入并保持撤消堆栈吗?
---------------- 根据要求更新代码 ----------
改用它,100% 有效。我测试它以确保安全。
private void TextBox_OnPreviewTextInput(object sender, TextCompositionEventArgs e)
{
if (e.Text == ";")
{
// In this line remove preview event to preventing event repeating
((TextBox)sender).PreviewTextInput -= TextBox_OnPreviewTextInput;
// Whith this code get the current index of you Caret(wher you inputed your semicolon)
int index = ((TextBox)sender).CaretIndex;
// Now do The Job in the new way( As you asked)
((TextBox)sender).Text = ((TextBox)sender).Text.Insert(index, ";\r\n");
// Give the Textbox preview Event again
((TextBox)sender).PreviewTextInput += TextBox_OnPreviewTextInput;
// Put the focus on the current index of TextBox after semicolon and newline (Updated Code & I think more optimized code)
((TextBox)sender).Select(index + 3, 0);
// Now enjoy your app
e.Handled = true;
}
}
祝你一切顺利,盖达尔
感谢 Heydar 提供解决方案。
我应用了一些改进:
private void TextBox_OnPreviewTextInput(object sender, TextCompositionEventArgs e)
{
if (e.Text == ";")
{
var textBox = (TextBox) sender;
var selectStart = textBox.SelectionStart;
var insertedText = ";" + Environment.NewLine;
// In this line remove preview event to preventing event repeating
textBox.PreviewTextInput -= TextBox_OnPreviewTextInput;
// Now do The Job
textBox.Text = textBox.Text.Insert(selectStart, insertedText);
// Give the TextBox preview Event again
textBox.PreviewTextInput += TextBox_OnPreviewTextInput;
// Put the focus after the inserted text
textBox.Select(selectStart + insertedText.Length, 0);
// Now enjoy your app
e.Handled = true;
}
}
当用户添加一个;
时,我想在TextBox中添加; + Environment.NewLine
。
我找到了这个解决方案:
private void TextBox_OnPreviewTextInput(object sender, TextCompositionEventArgs e)
{
if (e.Text == ";")
{
e.Handled = true;
TextCompositionManager.StartComposition(
new TextComposition(InputManager.Current,
(IInputElement)sender,
";" + Environment.NewLine)
);
}
}
但是在这之后,撤消就不起作用了。
你能解释一下如何控制用户输入并保持撤消堆栈吗?
---------------- 根据要求更新代码 ----------
改用它,100% 有效。我测试它以确保安全。
private void TextBox_OnPreviewTextInput(object sender, TextCompositionEventArgs e)
{
if (e.Text == ";")
{
// In this line remove preview event to preventing event repeating
((TextBox)sender).PreviewTextInput -= TextBox_OnPreviewTextInput;
// Whith this code get the current index of you Caret(wher you inputed your semicolon)
int index = ((TextBox)sender).CaretIndex;
// Now do The Job in the new way( As you asked)
((TextBox)sender).Text = ((TextBox)sender).Text.Insert(index, ";\r\n");
// Give the Textbox preview Event again
((TextBox)sender).PreviewTextInput += TextBox_OnPreviewTextInput;
// Put the focus on the current index of TextBox after semicolon and newline (Updated Code & I think more optimized code)
((TextBox)sender).Select(index + 3, 0);
// Now enjoy your app
e.Handled = true;
}
}
祝你一切顺利,盖达尔
感谢 Heydar 提供解决方案。 我应用了一些改进:
private void TextBox_OnPreviewTextInput(object sender, TextCompositionEventArgs e)
{
if (e.Text == ";")
{
var textBox = (TextBox) sender;
var selectStart = textBox.SelectionStart;
var insertedText = ";" + Environment.NewLine;
// In this line remove preview event to preventing event repeating
textBox.PreviewTextInput -= TextBox_OnPreviewTextInput;
// Now do The Job
textBox.Text = textBox.Text.Insert(selectStart, insertedText);
// Give the TextBox preview Event again
textBox.PreviewTextInput += TextBox_OnPreviewTextInput;
// Put the focus after the inserted text
textBox.Select(selectStart + insertedText.Length, 0);
// Now enjoy your app
e.Handled = true;
}
}