仅允许xaml.cs中的字符串值 [英] Allow only string values in xaml.cs

查看:114
本文介绍了仅允许xaml.cs中的字符串值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想允许用户只在文本框中输入字符串值,但我的代码运行不正确



  string  allowedCharacterSet =   bcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ \ b\\\
\t< /跨度>;

if (allowedCharacterSet.Contains(e.Key.ToString()))
{

}
else
{
e.Handled = true ;
}

解决方案

除了字符串值之外,用户已经完全无法输入任何内容。案例结束。



但是,如果您只想允许用户输入的某些字符,并且如果您正在谈论某些文本框,你应该处理事件 System.Windows.Controls.Primitives.TextBoxBase.TextChanged

http://msdn.microsoft.com/en-us/library/system.windows.controls.primitives.textboxbase。 textchanged.aspx [ ^ ]。



通过此活动,您有机会取消更改。请参阅:

http://msdn.microsoft .com / zh-cn / library / system.windows.controls.textchangedeventhandler.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/system.windows.controls.textchangedeventargs.aspx [ ^ ]。



以下是如何:你需要将属性 System.Windows.Controls.TextChangedEventArgs.Handled 分配给true:

http://msdn.microsoft.com/en-us/lib rary / system.windows.routedeventargs.handled.aspx [ ^ ]。



你还要犯一个错误:过滤退格,即一些历史原因,被视为一个人物。它的代码点是8.允许它。

祝你好运,

-SA

I want to allow user to only enter string values in textbox but my code is not working well

string allowedCharacterSet = "bcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\b\n\t";

        if (allowedCharacterSet.Contains(e.Key.ToString() ))
        {

        }
        else
        {
            e.Handled = true;
        }

解决方案

It is already totally impossible for a user to enter anything except a string value. Case closed.

However, if you want to allow only certain characters from your user input, and if you are talking about some text box, you should handle the event System.Windows.Controls.Primitives.TextBoxBase.TextChanged:
http://msdn.microsoft.com/en-us/library/system.windows.controls.primitives.textboxbase.textchanged.aspx[^].

With this event, you have the opportunity to cancel the change. Please see:
http://msdn.microsoft.com/en-us/library/system.windows.controls.textchangedeventhandler.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.controls.textchangedeventargs.aspx[^].

Here is how: you need to assign the property System.Windows.Controls.TextChangedEventArgs.Handled to true:
http://msdn.microsoft.com/en-us/library/system.windows.routedeventargs.handled.aspx[^].

You are about to make one more mistake: to filter out backspace, which is, by some historical reasons, is considered as a character. Its code point is 8. Allow it, too.
Good luck,
—SA


这篇关于仅允许xaml.cs中的字符串值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆