C# TextChanged事件(WPF Textbox)的奇怪错误
我有一个文本框,我试图用两种方式限制它: 1-我只允许数值,不允许小数C# TextChanged事件(WPF Textbox)的奇怪错误,c#,.net,wpf,C#,.net,Wpf,我有一个文本框,我试图用两种方式限制它: 1-我只允许数值,不允许小数 2-我只想接受您的代码不符合第一个条件的数字,因为 string.IsNullOrEmpty(textBoxWorkflowCount.Text) 计算结果为true,因此将转到“else”并显示消息 if (string.IsNullOrEmpty(textBoxWorkflowCount.Text) || Convert.ToInt32(textBoxWorkflowCount.Text) <= 35) e.H
2-我只想接受您的代码不符合第一个条件的数字,因为
string.IsNullOrEmpty(textBoxWorkflowCount.Text)
计算结果为true,因此将转到“else”并显示消息
if (string.IsNullOrEmpty(textBoxWorkflowCount.Text) || Convert.ToInt32(textBoxWorkflowCount.Text) <= 35) e.Handled = true;
if(string.IsNullOrEmpty(textBoxWorkflowCount.Text)| Convert.ToInt32(textBoxWorkflowCount.Text)几个月前,我发布了关于TextBox(Windows Phone 7.5平台)行为的帖子,其中一个是TextBoxInputRegexFilterBehavior,它允许您过滤输入文本。因此,如果您熟悉行为的工作方式,可以使用此代码
using System.Text.RegularExpressions;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Interactivity;
/// <summary>
/// UI behavior for <see cref="TextBox"/> to filter input text with special RegularExpression.
/// </summary>
public class TextBoxInputRegexFilterBehavior : Behavior<TextBox>
{
private Regex regex;
private string originalText;
private int originalSelectionStart;
private int originalSelectionLength;
/// <summary>
/// Gets or sets RegularExpression.
/// </summary>
public string RegularExpression
{
get
{
return this.regex.ToString();
}
set
{
if (string.IsNullOrEmpty(value))
{
this.regex = null;
}
else
{
this.regex = new Regex(value);
}
}
}
/// <inheritdoc/>
protected override void OnAttached()
{
base.OnAttached();
this.AssociatedObject.TextInputStart += this.AssociatedObjectTextInputStart;
this.AssociatedObject.TextChanged += this.AssociatedObjectTextChanged;
}
/// <inheritdoc/>
protected override void OnDetaching()
{
base.OnDetaching();
this.AssociatedObject.TextInputStart -= this.AssociatedObjectTextInputStart;
this.AssociatedObject.TextChanged -= this.AssociatedObjectTextChanged;
}
private void AssociatedObjectTextChanged(object sender, TextChangedEventArgs e)
{
if (this.originalText != null)
{
string text = this.originalText;
this.originalText = null;
this.AssociatedObject.Text = text;
this.AssociatedObject.Select(this.originalSelectionStart, this.originalSelectionLength);
}
}
private void AssociatedObjectTextInputStart(object sender, TextCompositionEventArgs e)
{
if (this.regex != null && e.Text != null && !(e.Text.Length == 1 && char.IsControl(e.Text[0])))
{
if (!this.regex.IsMatch(e.Text))
{
this.originalText = this.AssociatedObject.Text;
this.originalSelectionStart = this.AssociatedObject.SelectionStart;
this.originalSelectionLength = this.AssociatedObject.SelectionLength;
}
}
}
}
使用System.Text.regular表达式;
使用System.Windows.Controls;
使用System.Windows.Input;
使用System.Windows.Interactive;
///
///用于筛选具有特殊RegularExpression的输入文本的UI行为。
///
公共类TextBoxInputRegexFilterBehavior:行为
{
私人正则表达式正则表达式;
私有字符串源文本;
私人int原始选择启动;
private int originalSelectionLength;
///
///获取或设置正则表达式。
///
公共字符串正则表达式
{
得到
{
返回这个.regex.ToString();
}
设置
{
if(string.IsNullOrEmpty(value))
{
this.regex=null;
}
其他的
{
this.regex=新的regex(值);
}
}
}
///
受保护的覆盖无效附加()
{
base.onatached();
this.AssociatedObject.TextInputStart+=this.AssociatedObjectTextInputStart;
this.AssociatedObject.TextChanged+=this.AssociatedObjectTextChanged;
}
///
附加时受保护的覆盖无效()
{
base.OnDetaching();
this.AssociatedObject.TextInputStart-=this.AssociatedObjectTextInputStart;
this.AssociatedObject.TextChanged-=this.AssociatedObjectTextChanged;
}
与ObjectTextChanged关联的私有void(对象发送者,TextChangedEventArgs e)
{
如果(this.originalText!=null)
{
字符串文本=this.originalText;
this.originalText=null;
this.AssociatedObject.Text=文本;
this.AssociatedObject.Select(this.originalSelectionStart,this.originalSelectionLength);
}
}
私有void AssociatedObjectTextInputStart(对象发送者,TextCompositionEventArgs e)
{
如果(this.regex!=null&&e.Text!=null&&!(e.Text.Length==1&&char.IsControl(e.Text[0]))
{
如果(!this.regex.IsMatch(e.Text))
{
this.originalText=this.AssociatedObject.Text;
this.originalSelectionStart=this.associated object.SelectionStart;
this.originalSelectionLength=this.AssociatedObject.SelectionLength;
}
}
}
}
因此,通过这种行为,您可以编写简单的正则表达式来过滤用户输入,如RegularExpression=“(3[0-5])|([0-2]?[0-9])”哇,这太棒了-一定会用到。谢谢!
using System.Text.RegularExpressions;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Interactivity;
/// <summary>
/// UI behavior for <see cref="TextBox"/> to filter input text with special RegularExpression.
/// </summary>
public class TextBoxInputRegexFilterBehavior : Behavior<TextBox>
{
private Regex regex;
private string originalText;
private int originalSelectionStart;
private int originalSelectionLength;
/// <summary>
/// Gets or sets RegularExpression.
/// </summary>
public string RegularExpression
{
get
{
return this.regex.ToString();
}
set
{
if (string.IsNullOrEmpty(value))
{
this.regex = null;
}
else
{
this.regex = new Regex(value);
}
}
}
/// <inheritdoc/>
protected override void OnAttached()
{
base.OnAttached();
this.AssociatedObject.TextInputStart += this.AssociatedObjectTextInputStart;
this.AssociatedObject.TextChanged += this.AssociatedObjectTextChanged;
}
/// <inheritdoc/>
protected override void OnDetaching()
{
base.OnDetaching();
this.AssociatedObject.TextInputStart -= this.AssociatedObjectTextInputStart;
this.AssociatedObject.TextChanged -= this.AssociatedObjectTextChanged;
}
private void AssociatedObjectTextChanged(object sender, TextChangedEventArgs e)
{
if (this.originalText != null)
{
string text = this.originalText;
this.originalText = null;
this.AssociatedObject.Text = text;
this.AssociatedObject.Select(this.originalSelectionStart, this.originalSelectionLength);
}
}
private void AssociatedObjectTextInputStart(object sender, TextCompositionEventArgs e)
{
if (this.regex != null && e.Text != null && !(e.Text.Length == 1 && char.IsControl(e.Text[0])))
{
if (!this.regex.IsMatch(e.Text))
{
this.originalText = this.AssociatedObject.Text;
this.originalSelectionStart = this.AssociatedObject.SelectionStart;
this.originalSelectionLength = this.AssociatedObject.SelectionLength;
}
}
}
}