WPF(Windows Presentation Foundation)中的 TextBox
控件是用于输入和显示文本的基本控件。以下是 TextBox
控件的详细使用教程,包括其基本用法、常见属性、事件和高级功能。
1. 基本用法
在 WPF 中,你可以通过 XAML 或代码创建一个 TextBox
控件。
XAML 示例:
<Window x:Class="WpfApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="TextBox Example" Height="200" Width="300">
<Grid>
<TextBox Name="textBox1" Width="200" Height="30" Margin="20"/>
</Grid>
</Window>
代码示例(C#):
TextBox textBox1 = new TextBox();
textBox1.Width = 200;
textBox1.Height = 30;
textBox1.Margin = new Thickness(20);
this.Content = textBox1;
2. 常见属性
TextBox
控件有许多属性可以设置和调整其行为和外观。
- Text: 控件显示的文本内容。
<TextBox Text="Hello, World!" />
- IsReadOnly: 是否将
TextBox
设为只读模式。<TextBox IsReadOnly="True" Text="Read-only text" />
- MaxLength: 最大输入字符数。
<TextBox MaxLength="50" />
- AcceptsReturn: 是否允许在
TextBox
中输入多行文本(按下Enter
键时产生换行)。<TextBox AcceptsReturn="True" TextWrapping="Wrap" Height="100" />
- TextWrapping: 设置文本的换行模式(
Wrap
、NoWrap
)。<TextBox TextWrapping="Wrap" />
- HorizontalScrollBarVisibility 和 VerticalScrollBarVisibility: 设置水平和垂直滚动条的可见性(
Disabled
、Auto
、Hidden
、Visible
)。<TextBox VerticalScrollBarVisibility="Auto" />
3. 事件处理
TextBox
控件可以处理各种事件来响应用户输入。
-
TextChanged: 当
TextBox
中的文本改变时触发。<TextBox Name="textBox1" TextChanged="textBox1_TextChanged" />
private void textBox1_TextChanged(object sender, TextChangedEventArgs e) { // 在文本改变时执行的逻辑 MessageBox.Show("Text has changed!"); }
-
GotFocus 和 LostFocus: 当
TextBox
获得或失去焦点时触发。<TextBox Name="textBox1" GotFocus="textBox1_GotFocus" LostFocus="textBox1_LostFocus" />
private void textBox1_GotFocus(object sender, RoutedEventArgs e) { textBox1.Background = Brushes.LightYellow; } private void textBox1_LostFocus(object sender, RoutedEventArgs e) { textBox1.Background = Brushes.White; }
-
KeyDown 和 KeyUp: 当用户在
TextBox
中按下或释放键时触发。<TextBox Name="textBox1" KeyDown="textBox1_KeyDown" />
private void textBox1_KeyDown(object sender, KeyEventArgs e) { if (e.Key == Key.Enter) { MessageBox.Show("Enter key was pressed!"); } }
4. 高级功能
-
数据绑定:
TextBox
的Text
属性通常与数据源绑定,以便在数据变化时自动更新。<TextBox Text="{Binding Path=UserName}" />
-
样式和模板:你可以通过设置样式和模板来自定义
TextBox
的外观。<TextBox Width="200" Height="30"> <TextBox.Style> <Style TargetType="TextBox"> <Setter Property="Foreground" Value="Blue"/> <Setter Property="FontSize" Value="16"/> </Style> </TextBox.Style> </TextBox>
-
输入验证:使用
Validation
类,可以在TextBox
上实现数据输入验证,并在输入无效时显示错误提示。<TextBox Name="textBox1"> <TextBox.Text> <Binding Path="Age" UpdateSourceTrigger="PropertyChanged"> <Binding.ValidationRules> <local:AgeValidationRule /> </Binding.ValidationRules> </Binding> </TextBox.Text> </TextBox>
-
多行输入:通过设置
AcceptsReturn
为True
和TextWrapping
为Wrap
,可以让TextBox
支持多行输入和自动换行。<TextBox AcceptsReturn="True" TextWrapping="Wrap" Height="100" />
-
密码输入:虽然 WPF 提供了
PasswordBox
控件用于密码输入,但你也可以通过设置TextBox
的PasswordChar
属性模拟密码输入(需要自定义)。
5. 扩展功能
-
Watermark(水印):可以使用
AdornerDecorator
或第三方库添加水印效果,提示用户输入内容。 -
输入限制:通过处理
PreviewTextInput
事件,可以限制TextBox
仅允许输入特定类型的字符。private void textBox1_PreviewTextInput(object sender, TextCompositionEventArgs e) { e.Handled = !IsTextAllowed(e.Text); } private bool IsTextAllowed(string text) { // 仅允许数字输入 return Regex.IsMatch(text, "^[0-9]+$"); }
6. 实践建议
- 性能优化:在处理大量文本时,可以通过优化
TextBox
的TextChanged
事件处理逻辑来提高性能。 - 可访问性:为
TextBox
添加适当的AutomationProperties
属性,提升应用程序的可访问性。
通过灵活使用这些功能,TextBox
控件可以满足大部分文本输入的需求。你可以根据具体场景定制 TextBox
的行为和外观,从而提供更好的用户体验。