WPF的UI元素类型
| 名称 | 注释 |
|---|---|
| ContentControl | 单一内容控件 |
| HeaderedContentControl | 带标题的单一内容控件 |
| ItemsControl | 以条目集合为内容的控件 |
| HedderedItemsControl | 带标题的条目集合内容控件 |
| Decorator | 控件装饰元素 |
| Panel | 面板类元素 |
| Adorner | 文字点缀元素 |
| Flow Text | 流式文本元素 |
| TextBox | 文本输入 |
| TextBlock | 静态文字 |
| Shape | 图形 |
包含控件:
| Button | ButtonBase | CheckBox | ComboBoxItem | ContentControl |
|---|---|---|---|---|
| GroupItem | Label | ListBoxItem | ListViewItem | NavigationWindow |
| RepeatButton | ScrollViewer | StatusBarItem | ToggleButton | ToolTip |
| UserControl | Window |
包含控件:
| Expander | GroupBox | HeaderedContentControl | TabItem |
|---|
| Menu | MenuBase | ContextMenu | ComboBox | ItemsControl | ListBox |
|---|---|---|---|---|---|
| ListView | TabControl | TreeView | Selector | StatusBar |
该类控件会自动使用相应的条目容器对提交给它的内容进行包装,合法的ItemsControl内容一定是集合,这个集合不会直接个给ItemsControl使用,而是先使用条目容器进行包装,这样的好处是允许程序员向ItemsControl提交各种类型的数据集合。
public class Person {public int Id { set; get; }public string Name { set; get; }
}List people = new List(){new Person(){Id=1,Name="a"},new Person(){Id=2,Name="b"},new Person(){Id=3,Name="c"},new Person(){Id=4,Name="d"},};//DisplayMebberPath只能显示字符串,更复杂的数据使用DataTemplatethis.lst.DisplayMemberPath = "Name";//显示数据的那个属性,listBox会调用这个属性的ToString()方法this.lst.SelectedValuePath = "Id";//与SelectedValue属性配合使用,作为value的键this.lst.ItemsSource = people;
内容属性为Items、ItemsSource、Header
| MenuItem | TreeViewItem | ToolBar |
|---|
在UI上起装饰效果
| ButtonChrome | ClassicBorderDecorator | ListBoxChrome | SystemDropShadowChrome | Border |
|---|---|---|---|---|
| InkPresenter | BulletDecorator | Viewbox | AdornerDecorator |
包括控件:
| Canvas | DockPanel | Grid | TabPenel | ToolBarOVerFlowPanel | StackPanel |
|---|---|---|---|---|---|
| ToolBarPanel | UniformGrid | VirtualizingPanel | VirtualizingStackPanel | WrapPanel |
Grid适合场景:

通过代码
this.grid.ColumnDefinitions.Add(new ColumnDefinition());
this.grid.RowDefinitions.Add(new RowDefinition());
| 英文名称 | 中文名称 | 简写 | 换算 |
|---|---|---|---|
| Pixel | 像素 | px | 图形基本单位 |
| Inch | 英寸 | in | 1in=96px |
| Centimeter | 厘米 | cm | 1cm=(96/2.54)px |
| Point | 点 | pt | 1pt=(96/72)px |
值类型:
案例:
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-RoDZhhb6-1667201664153)(4.控件与布局.assets/image-20221031145451855.png)]](https://img.pic99.top/hhfamen/202404/f5a03644dad6110.png)
内部元素可以横向和纵向紧凑排列,删除一个元素后面的自动移动到前面
适用场景:
常用的3个属性
| 属性名称 | 数据类型 | 可取值 | 描述 |
|---|---|---|---|
| Orientation | Orientation枚举 | Horizongtal、Vertial | 横向排列还是纵向排列 |
| HorizontalAlignment | HorizontalAlignment枚举 | Left、Center、Right、Stretch | 内部元素水平方向上的对齐方式 |
| VerticalAlignment | VerticalAlignment枚举 | Top、Center、Bottom、Stretch | 内部元素竖直方向上的对齐方式 |
类似于Winform
使用场景:
内部采用流式布局,使用Orientation属性来控制流延伸的方向,使用HorizontalAlignment和VerticalAlignment两个属性控制内部控件的对齐。