は、あなたの目標に応じて、別のオプションは、PreviewTextInput
を処理することです:あなたはコピーペーストスペースを停止する場合
例えば
{
//...
TextBox tb = new TextBox();
tb.PreviewTextInput += Tb_PreviewTextInput;
}
private void Tb_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
if (e.Text == " ")
e.Handled = true;
base.OnPreviewTextInput(e);
}
、これが推奨されます。
あなたがテキストボックスに挿入する前に、テキストからすべてのスペースを削除する状況を指している一方、次のことが役立つかもしれない。この場合
// ...
TextBox tb = new TextBox();
tb.TextChanged += Tb_TextChanged;
// ...
bool _changing;
private void Tb_TextChanged(object sender, TextChangedEventArgs e)
{
if (_changing)
return;
_changing = true;
TextBox tb = (TextBox)sender;
string tx = tb.Text;
while (tx.Contains(" "))
tx = tx.Replace(" ", string.Empty);
tb.Text = tx;
_changing = false;
}
を、this linkを参照してください、あまりにも。
出典
2017-02-16 10:58:37
Ron
また、私はtryを試してみましたが、何も変わっていません。テキストボックスなどは、テキストボックス内のスペースを受け入れる – WPFwtf
[Dublicate](http://stackoverflow.com/questions/10410882/how-can-i-modify-this-regular-expression-to-not-allow-white-spaces )? – Shakra
「KeyPress」イベントを使用し、Keyが32またはKey.Spaceの場合、e.Handled = true; –