【发布时间】:2017-07-26 18:13:15
【问题描述】:
public static void AlphabeticPasting(object sender, DataObjectPastingEventArgs e)
{
if (e.DataObject.GetDataPresent(typeof(string)))
{
var text = e.DataObject.GetData(typeof(string)) as string;
if (!Regex.IsMatch(text, (Properties.Resources.Alpha)))
{
//replace and recopy
var trimmed = Regex.Replace(text,Properties.Resources.AlphaPasting, string.Empty);
e.CancelCommand();
Clipboard.SetData(DataFormats.Text, trimmed);
ApplicationCommands.Paste.Execute(trimmed, e.Source as FrameworkElement);
}
}
}
我想限制数字部分粘贴到文本框中, 我在文本框的 DataObject.Pasting 属性中调用此方法,Properties.Resources.Alphabet 和 Properties.Resources.AlphabetPasting 是正则表达式部分,我在资源文件中提到了正则表达式方程。
Alpha = ^[[A-Za-z-~`!@#$%^&*()_+=|{}':;.,<>/\\]?]*$ and AlphaPsting = [^[A-Za-z-~`!@#$%^&*()_+=|{}':;.,<>/\\]?]+
我只需要从资源文件中获取正则表达式。
如果我尝试跳过文本框中的任何内容,我会收到 System.StackOverFlowException。请帮我解决这个问题。
【问题讨论】:
-
如果您正在处理AlphabeticPasting 的事件,是不是在您执行ApplicationCommand.Paste.Execute 时再次调用它?这可能会导致它...