您可以使用以下代码来限制一个TextBox控件最多只能输入10个字符:\n\ncsharp\nprivate void TextBox_TextChanged(object sender, TextChangedEventArgs e)\n{\n TextBox textBox = (TextBox)sender;\n if (textBox.Text.Length > 10)\n {\n textBox.Text = textBox.Text.Substring(0, 10);\n textBox.CaretIndex = textBox.Text.Length; // 将光标移动到文本末尾\n }\n}\n\n\n在XAML中,将TextBox的TextChanged事件绑定到上述的事件处理程序:\n\nxaml\n<TextBox TextChanged="TextBox_TextChanged" />\n\n\n这样,当用户在TextBox中输入文本时,如果超过10个字符,代码将自动截断文本至10个字符,并将光标移动到文本末尾。

WPF限制TextBox输入字符数量 - 最多10个字符

原文地址: https://www.cveoy.top/t/topic/pZTB 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录