WPF 常用控件及其属性方法大全

 
 
 以下是 WPF 中常用的控件及其核心属性、方法和事件:

1. Button (按钮)

常用属性:

  • Content - 按钮显示的内容(可以是文本或其他元素)

  • IsEnabled - 是否启用按钮

  • Background/Foreground - 背景色/前景色

  • BorderBrush/BorderThickness - 边框颜色/粗细

  • Padding - 内边距

  • Command - 绑定的命令

  • CommandParameter - 命令参数

常用事件:

  • Click - 点击事件

<Button Content="点击我" Click="Button_Click"/>

2. TextBox (文本框)

常用属性:

  • Text - 文本框内容

  • MaxLength - 最大字符数

  • TextWrapping - 文本换行方式

  • IsReadOnly - 是否只读

  • SelectionStart/SelectionLength - 选中文本

  • AcceptsReturn - 是否接受回车

  • AcceptsTab - 是否接受制表符

常用方法:

  • Clear() - 清空文本

  • SelectAll() - 全选文本

  • AppendText(string) - 追加文本

常用事件:

  • TextChanged - 文本改变时触发

  • PreviewTextInput - 文本输入前触发

  • LostFocus - 失去焦点时触发

3. Label (标签)

常用属性:

  • Content - 显示内容

  • Target - 指定关联的控件(设置助记键时使用)

<Label Content="_Username:" Target="{Binding ElementName=userNameTextBox}"/>
<TextBox x:Name="userNameTextBox"/>

4. ComboBox (下拉框)

常用属性:

  • ItemsSource - 数据源

  • SelectedItem - 当前选中项

  • SelectedIndex - 当前选中索引

  • DisplayMemberPath - 显示成员的路径

  • IsEditable - 是否可编辑

  • Text - 编辑框中的文本(当IsEditable=true时)

常用方法:

  • Add(object) - 添加项

  • Remove(object) - 移除项

  • Clear() - 清空所有项

常用事件:

  • SelectionChanged - 选择项改变时触发

5. ListBox (列表框)

常用属性:

  • ItemsSource - 数据源

  • SelectedItem - 当前选中项

  • SelectedItems - 当前选中的多项(当SelectionMode为Multiple或Extended时)

  • SelectionMode - 选择模式(Single/Multiple/Extended)

  • DisplayMemberPath - 显示成员的路径

常用方法:

  • ScrollIntoView(object) - 滚动到指定项

常用事件:

  • SelectionChanged - 选择项改变时触发

6. CheckBox (复选框)

常用属性:

  • IsChecked - 是否选中(bool?,支持三种状态)

  • Content - 显示内容

  • ThreeState - 是否支持三种状态

常用事件:

  • Checked/Unchecked - 选中状态改变时触发

  • Indeterminate - 不确定状态时触发

7. RadioButton (单选按钮)

常用属性:

  • IsChecked - 是否选中

  • Content - 显示内容

  • GroupName - 分组名称(同组内互斥)

常用事件:

  • Checked - 选中时触发

8. Slider (滑块)

常用属性:

  • Minimum/Maximum - 最小/最大值

  • Value - 当前值

  • TickFrequency - 刻度间隔

  • IsSnapToTickEnabled - 是否吸附到刻度

  • Orientation - 方向(Horizontal/Vertical)

常用事件:

  • ValueChanged - 值改变时触发

9. ProgressBar (进度条)

常用属性:

  • Minimum/Maximum - 最小/最大值

  • Value - 当前值

  • IsIndeterminate - 是否为不确定状态(动态显示)

10. Image (图像)

常用属性:

  • Source - 图像源(BitmapImage/Uri)

  • Stretch - 拉伸方式(None/Fill/Uniform/UniformToFill)

  • StretchDirection - 拉伸方向

 

<Image Source="Images/logo.png" Stretch="Uniform"/>

11. DatePicker (日期选择器)

常用属性:

  • SelectedDate - 选择的日期(DateTime?)

  • DisplayDate - 显示的日期

  • DisplayDateStart/DisplayDateEnd - 可选日期范围

  • IsDropDownOpen - 下拉框是否打开

常用事件:

  • SelectedDateChanged - 日期改变时触发

12. TreeView (树形视图)

常用属性:

  • ItemsSource - 数据源

  • SelectedItem - 当前选中项

常用方法:

  • ExpandSubtree() - 展开所有子项

常用事件:

  • SelectedItemChanged - 选中项改变时触发

13. DataGrid (数据表格)

常用属性:

  • ItemsSource - 数据源

  • SelectedItem - 当前选中行

  • AutoGenerateColumns - 是否自动生成列

  • CanUserAddRows - 用户是否可以添加行

  • CanUserDeleteRows - 用户是否可以删除行

常用方法:

  • BeginEdit() - 开始编辑

  • CommitEdit() - 提交编辑

  • CancelEdit() - 取消编辑

常用事件:

  • SelectionChanged - 选择改变时触发

  • RowEditEnding - 行编辑结束时触发

14. TabControl (选项卡)

常用属性:

  • SelectedIndex - 当前选中标签索引

  • SelectedItem - 当前选中标签项

常用事件:

  • SelectionChanged - 标签切换时触发

15. Menu (菜单)

常用属性:

  • Items - 菜单项集合

常用方法:

  • AddMenuItem() - 添加菜单项

16. ContextMenu (上下文菜单)

常用属性:

  • Items - 菜单项集合

  • IsOpen - 菜单是否打开

17. ToolTip (工具提示)

常用属性:

  • Content - 提示内容

  • Placement - 提示位置

  • IsOpen - 是否显示

18. ScrollViewer (滚动视图)

常用属性:

  • HorizontalScrollBarVisibility - 水平滚动条可见性

  • VerticalScrollBarVisibility - 垂直滚动条可见性

  • Content - 滚动内容

19. Border (边框)

常用属性:

  • BorderBrush - 边框颜色

  • BorderThickness - 边框粗细

  • CornerRadius - 圆角半径

  • Background - 背景色

20. Expander (可展开控件)

常用属性:

  • IsExpanded - 是否展开

  • Header - 标题内容

常用事件:

  • Expanded/Collapsed - 展开/折叠时触发

通用控件属性

大多数控件共享以下通用属性:

  • Visibility - 可见性(Visible/Collapsed/Hidden)

  • Width/Height - 宽度/高度

  • MinWidth/MinHeight - 最小尺寸

  • MaxWidth/MaxHeight - 最大尺寸

  • Margin - 外边距

  • Padding - 内边距

  • HorizontalAlignment/VerticalAlignment - 对齐方式

  • Style - 样式

  • DataContext - 数据上下文

posted @ 2025-06-14 14:39  若水如引  阅读(397)  评论(0)    收藏  举报