C# WPF中的屏蔽文本框格式
我正在为WPF做一个文本框屏蔽。删除文本框文本中的字符行时出错 我想做的是C# WPF中的屏蔽文本框格式,c#,wpf,xaml,C#,Wpf,Xaml,我正在为WPF做一个文本框屏蔽。删除文本框文本中的字符行时出错 我想做的是 { class MyMaskedTextBox : TextBox { private System.ComponentModel.MaskedTextProvider _mprovider = null; public string Mask { get { if (_mprovider != null) return _mprovider
{
class MyMaskedTextBox : TextBox
{
private System.ComponentModel.MaskedTextProvider _mprovider = null;
public string Mask
{
get
{
if (_mprovider != null) return _mprovider.Mask;
else return "";
}
set
{
_mprovider = new System.ComponentModel.MaskedTextProvider(value);
this.Text = _mprovider.ToDisplayString();
}
}
private bool PreviousInsertState = false;
private bool _InsertIsON = false;
private bool _stayInFocusUntilValid = true;
/// <summary>
/// Sets whether the focus should stay on the control until the contents are valid
/// </summary>
public bool StayInFocusUntilValid
{
get { return _stayInFocusUntilValid; }
set { _stayInFocusUntilValid = value; }
}
private bool _NewTextIsOk = false;
/// <summary>
/// Defines whether the next entered input text is ok according to the mask
/// </summary>
public bool NewTextIsOk
{
get { return _NewTextIsOk; }
set { _NewTextIsOk = value; }
}
private bool _ignoreSpace = true;
/// <summary>
/// Sets whether space should be ignored
/// </summary>
public bool IgnoreSpace
{
get { return _ignoreSpace; }
set { _ignoreSpace = value; }
}
/// <summary>
/// Stops the effect of some common keys
/// </summary>
/// <param name="e"></param>
protected override void OnPreviewKeyDown(System.Windows.Input.KeyEventArgs e)
{
if (this.SelectionLength > 1)
{
this.SelectionLength = 0;
e.Handled = false;
}
if (e.Key == Key.Insert || e.Key == Key.Delete || e.Key == Key.Back || (e.Key == Key.Space && _ignoreSpace))
{
e.Handled = false;
}
base.OnPreviewKeyDown(e);
}
/// <summary>
/// We check whether we are ok
/// </summary>
/// <param name="e"></param>
protected override void OnPreviewTextInput(System.Windows.Input.TextCompositionEventArgs e)
{
System.ComponentModel.MaskedTextResultHint hint;
int TestPosition;
if (e.Text.Length == 1)
this._NewTextIsOk = _mprovider.VerifyChar(e.Text[0], this.CaretIndex, out hint);
else
this._NewTextIsOk = _mprovider.VerifyString(e.Text, out TestPosition, out hint);
base.OnPreviewTextInput(e);
}
/// <summary>
/// When text is received by the TextBox we check whether to accept it or not
/// </summary>
/// <param name="e"></param>
protected override void OnTextInput(System.Windows.Input.TextCompositionEventArgs e)
{
string PreviousText = this.Text;
if (NewTextIsOk)
{
base.OnTextInput(e);
if (_mprovider.VerifyString(this.Text) == false) this.Text = PreviousText;
while (!_mprovider.IsEditPosition(this.CaretIndex) && _mprovider.Length > this.CaretIndex) this.CaretIndex++;
}
else
e.Handled = false; //true;
}
/// <summary>
/// When the TextBox takes the focus we make sure that the Insert is set to Replace
/// </summary>
/// <param name="e"></param>
protected override void OnGotFocus(RoutedEventArgs e)
{
base.OnGotFocus(e);
if (!_InsertIsON)
{
PressKey(Key.Insert);
_InsertIsON = true;
}
}
/// <summary>
/// When the textbox looses the keyboard focus we may want to verify (based on the StayInFocusUntilValid) whether
/// the control has a valid value (fully complete)
/// </summary>
/// <param name="e"></param>
protected override void OnPreviewLostKeyboardFocus(KeyboardFocusChangedEventArgs e)
{
if (StayInFocusUntilValid)
{
_mprovider.Clear();
_mprovider.Add(this.Text);
if (!_mprovider.MaskFull) e.Handled = true;
}
base.OnPreviewLostKeyboardFocus(e);
}
/// <summary>
/// Simulates pressing a key
/// </summary>
/// <param name="key">The key to be pressed</param>
private void PressKey(Key key)
{
KeyEventArgs eInsertBack = new KeyEventArgs(Keyboard.PrimaryDevice,
Keyboard.PrimaryDevice.ActiveSource,
0, key);
eInsertBack.RoutedEvent = KeyDownEvent;
InputManager.Current.ProcessInput(eInsertBack);
}
}
我有一个掩蔽格式,比如XXXX/XXXX/XXXX/XXXX
如果用户在输入错误时删除了文本框内容,我希望将XXXX/XXXX/XXXX/XXXX/XXXX字符自动返回到文本框
我可以将用户删除分组吗
例如,格式XXXX-YYYY-ZZZZ-TTTT。我可以按X、Y、Y、T值删除组吗
main window.xaml
<StackPanel Orientation="Horizontal" Width="280">
<local:MyMaskedTextBox x:Name="MaskedDemo" Mask="0000/0000/0000/0000" StayInFocusUntilValid="True" IgnoreSpace="True" Width="180" Height="26" Margin="20"/>
<Button Content="OK" Height="24" Width="50"/>
</StackPanel>
MyMaskedTextBox.cs
{
class MyMaskedTextBox : TextBox
{
private System.ComponentModel.MaskedTextProvider _mprovider = null;
public string Mask
{
get
{
if (_mprovider != null) return _mprovider.Mask;
else return "";
}
set
{
_mprovider = new System.ComponentModel.MaskedTextProvider(value);
this.Text = _mprovider.ToDisplayString();
}
}
private bool PreviousInsertState = false;
private bool _InsertIsON = false;
private bool _stayInFocusUntilValid = true;
/// <summary>
/// Sets whether the focus should stay on the control until the contents are valid
/// </summary>
public bool StayInFocusUntilValid
{
get { return _stayInFocusUntilValid; }
set { _stayInFocusUntilValid = value; }
}
private bool _NewTextIsOk = false;
/// <summary>
/// Defines whether the next entered input text is ok according to the mask
/// </summary>
public bool NewTextIsOk
{
get { return _NewTextIsOk; }
set { _NewTextIsOk = value; }
}
private bool _ignoreSpace = true;
/// <summary>
/// Sets whether space should be ignored
/// </summary>
public bool IgnoreSpace
{
get { return _ignoreSpace; }
set { _ignoreSpace = value; }
}
/// <summary>
/// Stops the effect of some common keys
/// </summary>
/// <param name="e"></param>
protected override void OnPreviewKeyDown(System.Windows.Input.KeyEventArgs e)
{
if (this.SelectionLength > 1)
{
this.SelectionLength = 0;
e.Handled = false;
}
if (e.Key == Key.Insert || e.Key == Key.Delete || e.Key == Key.Back || (e.Key == Key.Space && _ignoreSpace))
{
e.Handled = false;
}
base.OnPreviewKeyDown(e);
}
/// <summary>
/// We check whether we are ok
/// </summary>
/// <param name="e"></param>
protected override void OnPreviewTextInput(System.Windows.Input.TextCompositionEventArgs e)
{
System.ComponentModel.MaskedTextResultHint hint;
int TestPosition;
if (e.Text.Length == 1)
this._NewTextIsOk = _mprovider.VerifyChar(e.Text[0], this.CaretIndex, out hint);
else
this._NewTextIsOk = _mprovider.VerifyString(e.Text, out TestPosition, out hint);
base.OnPreviewTextInput(e);
}
/// <summary>
/// When text is received by the TextBox we check whether to accept it or not
/// </summary>
/// <param name="e"></param>
protected override void OnTextInput(System.Windows.Input.TextCompositionEventArgs e)
{
string PreviousText = this.Text;
if (NewTextIsOk)
{
base.OnTextInput(e);
if (_mprovider.VerifyString(this.Text) == false) this.Text = PreviousText;
while (!_mprovider.IsEditPosition(this.CaretIndex) && _mprovider.Length > this.CaretIndex) this.CaretIndex++;
}
else
e.Handled = false; //true;
}
/// <summary>
/// When the TextBox takes the focus we make sure that the Insert is set to Replace
/// </summary>
/// <param name="e"></param>
protected override void OnGotFocus(RoutedEventArgs e)
{
base.OnGotFocus(e);
if (!_InsertIsON)
{
PressKey(Key.Insert);
_InsertIsON = true;
}
}
/// <summary>
/// When the textbox looses the keyboard focus we may want to verify (based on the StayInFocusUntilValid) whether
/// the control has a valid value (fully complete)
/// </summary>
/// <param name="e"></param>
protected override void OnPreviewLostKeyboardFocus(KeyboardFocusChangedEventArgs e)
{
if (StayInFocusUntilValid)
{
_mprovider.Clear();
_mprovider.Add(this.Text);
if (!_mprovider.MaskFull) e.Handled = true;
}
base.OnPreviewLostKeyboardFocus(e);
}
/// <summary>
/// Simulates pressing a key
/// </summary>
/// <param name="key">The key to be pressed</param>
private void PressKey(Key key)
{
KeyEventArgs eInsertBack = new KeyEventArgs(Keyboard.PrimaryDevice,
Keyboard.PrimaryDevice.ActiveSource,
0, key);
eInsertBack.RoutedEvent = KeyDownEvent;
InputManager.Current.ProcessInput(eInsertBack);
}
}
{
类MyMaskedTextBox:TextBox
{
private System.ComponentModel.MaskedTextProvider\u mprovider=null;
公共字符串掩码
{
得到
{
如果(_mprovider!=null)返回_mprovider.Mask;
否则返回“”;
}
设置
{
_MPProvider=新系统.ComponentModel.MaskedTextProvider(值);
this.Text=\u mprovider.ToDisplayString();
}
}
private bool PreviousInsertState=false;
private bool_InsertIsON=false;
private boolu stayInFocusUntilValid=true;
///
///设置在内容有效之前焦点是否应保留在控件上
///
公共图书馆
{
获取{return\u stayInFocusUntilValid;}
设置{u stayInFocusUntilValid=value;}
}
private bool_NewTextIsOk=false;
///
///根据掩码定义下一个输入文本是否正常
///
公共图书馆
{
获取{return\u NewTextIsOk;}
设置{u NewTextIsOk=value;}
}
私有bool_ignoreSpace=true;
///
///设置是否应忽略空格
///
公共广播信号空间
{
获取{return\u ignoreSpace;}
设置{u ignoreSpace=value;}
}
///
///停止某些常用键的效果
///
///
受保护的覆盖无效OnPreviewKeyDown(System.Windows.Input.KeyEventArgs e)
{
如果(this.SelectionLength>1)
{
this.SelectionLength=0;
e、 已处理=错误;
}
if(e.Key==Key.Insert | e.Key==Key.Delete | e.Key==Key.Back | |(e.Key==Key.Space&&ignoreSpace))
{
e、 已处理=错误;
}
基于预览的向下(e);
}
///
///我们检查一下是否正常
///
///
受保护的覆盖在PreviewTestInput上无效(System.Windows.Input.TextCompositionEventArgs e)
{
System.ComponentModel.MaskedTextResultHint提示;
int测试位置;
如果(e.Text.Length==1)
this.\u NewTextIsOk=\u mprovider.VerifyChar(e.Text[0],this.CaretIndex,out-hint);
其他的
此.\u NewTextIsOk=\u mprovider.VerifyString(例如,Text,out TestPosition,out hint);
基于输出(e);
}
///
///当文本框收到文本时,我们检查是否接受它
///
///
受保护的覆盖无效输入(System.Windows.Input.TextCompositionEventArgs e)
{
字符串PreviousText=此.Text;
if(NewTextIsOk)
{
基本输出(e);
如果(\u mprovider.VerifyString(this.Text)=false)this.Text=PreviousText;
而(!\mprovider.IsEditPosition(this.CaretIndex)和&\mprovider.Length>this.CaretIndex)this.CaretIndex++;
}
其他的
e、 Handled=false;//true;
}
///
///当文本框获得焦点时,我们确保插入设置为替换
///
///
受保护的覆盖无效OnGotFocus(路由目标e)
{
基地,昂戈特福克斯(e);;
如果(!\u插入)
{
按按键(键。插入);
_InsertIsON=true;
}
}
///
///当文本框松开键盘焦点时,我们可能需要验证(基于StayInFocusUntilValid)是否
///控件具有有效值(完全完成)
///
///
PreviewLostKeyboardFocus上的受保护覆盖无效(KeyboardFocusChangedEventArgs e)
{
if(StayInFocusUntilValid)
{
_mprovider.Clear();
_mprovider.Add(this.Text);
如果(!\u mprovider.MaskFull)e.Handled=true;
}
基于PreviewLostKeyboardFocus(e);
}
///
///模拟按下一个键
///
///要按下的键
专用无效按键(按键)
{
KeyEventArgs eInsertBack=新的KeyEventArgs(Keyboard.PrimaryDevice,
Keyboard.PrimaryDevice.ActiveSource,
0,键);
eInsertBack.RoutedEvent=KeyDownEvent;
InputManager.Current.ProcessInput(eInsertBack);
}
}
}
我在某处犯了个错误。错误在哪里?这是一个解决方案,它不允许删除分隔标记,并且当您删除一些数字时,它将替换为“\ux”。改变一种方法。 同样在if for delete key内,即Handled设置为true,表示不执行删除,只在if语句内实现逻辑
protected override void OnPreviewKeyDown(System.Windows.Input.KeyEventArgs e)
{
if (this.SelectionLength > 1)
{
this.SelectionLength = 0;
e.Handled = false;
}
if (e.Key == Key.Insert || e.Key == Key.Delete || e.Key == Key.Back || (e.Key == Key.Space && _ignoreSpace))
{
StringBuilder sb = new StringBuilder(this.Text);
if (sb[CaretIndex - 1] != '-')
{
sb[CaretIndex - 1] = '_';
}
this.Text = sb.ToString();
e.Handled = true;
}
base.OnPreviewKeyDown(e);
}
请告诉我解决方案是否有效!。它对我有用 出现什么错误?我无法再次在文本框中输入值,因为当我在文本框中输入不正确的值并将其删除时,掩码对象(XXXX-XXXX-XXXX-XXXX)被删除,但您希望用户可以删除其他数字,对吗?仅不允许删除“/”标记?屏蔽过程中不会自动加载括号。如果用户删除四重奏组之间的分隔符,例如/或-,则屏蔽操作是错误的,并且@KasparYes无法再次执行数据输入操作,但通常屏蔽中不允许执行删除操作,我想知道这是否是您的解决方案。所以用户不能删除