Silverlight 4 中的过滤膏

发布于 2024-11-29 01:01:24 字数 84 浏览 7 评论 0原文

我有 TextBox ,它只允许插入数值(过滤),但是当我粘贴复制的文本时,它允许任何类型的符号。如何在粘贴之前阻止或过滤文本?

I have TextBox which allow insert only numeric values (filtering), But when I paste copied text it's allow any kind of symbol. How can I prevent or filter text before pasting?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

时光匆匆的小流年 2024-12-06 01:01:24

您可以在任何手动输入之前备份文本,然后当提供的输入无效时恢复以前的文本,如下所示:

        _backupText = string.Empty;
        doNotPasteTextBox.TextInputStart += (sender, e) =>
                                                {
                                                    int textParsed;
                                                    if(int.TryParse(e.Text,out textParsed))
                                                    {
                                                        _backupText = doNotPasteTextBox.Text.Insert(doNotPasteTextBox.SelectionStart, e.Text);
                                                    }else
                                                    {
                                                        e.Handled = true;
                                                    }
                                                };

        doNotPasteTextBox.TextChanged += (sender, e) =>
                                             {
                                                 int textParsed;
                                                 int selectionStart = doNotPasteTextBox.SelectionStart;
                                                 if(!int.TryParse(doNotPasteTextBox.Text, out textParsed))
                                                 {
                                                     doNotPasteTextBox.Text = _backupText;
                                                 }
                                                 doNotPasteTextBox.SelectionStart = selectionStart;
                                             };

我不建议 尝试捕获控制键或任何东西,因为当你在 Mac 或 Linux 上时,你就完蛋了。

调整我的样本并将其倒入新的文本框控件中以使其更干净,但您明白了。

You could backup your text before any manual input and then when the input provided isn't valid restore the previous text like so:

        _backupText = string.Empty;
        doNotPasteTextBox.TextInputStart += (sender, e) =>
                                                {
                                                    int textParsed;
                                                    if(int.TryParse(e.Text,out textParsed))
                                                    {
                                                        _backupText = doNotPasteTextBox.Text.Insert(doNotPasteTextBox.SelectionStart, e.Text);
                                                    }else
                                                    {
                                                        e.Handled = true;
                                                    }
                                                };

        doNotPasteTextBox.TextChanged += (sender, e) =>
                                             {
                                                 int textParsed;
                                                 int selectionStart = doNotPasteTextBox.SelectionStart;
                                                 if(!int.TryParse(doNotPasteTextBox.Text, out textParsed))
                                                 {
                                                     doNotPasteTextBox.Text = _backupText;
                                                 }
                                                 doNotPasteTextBox.SelectionStart = selectionStart;
                                             };

I wouldn't recommend trying to capture the control keys or anything because when you're on a mac or on linux then you're screwed.

Adjust my sample and pour it inside a new textbox control to make it cleaner but you get the idea.

爱冒险 2024-12-06 01:01:24

您可以使用 Clipboard.GetText() 获取插入到文本框中的文本,但这会弹出一条消息,并且用户必须授予应用程序访问剪贴板的权限。

如果对你来说没问题那么我会用这个。

You could use Clipboard.GetText() to get the text that is inserted into the textbox, but this will pop up a message, and the user must give the application access to the Clipboard.

If its no problem for you then i would use this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文