WPF核心控件属性信息表

OpenTK 2026-01-15 14:53:49
控件名称属性分类及完整属性列表(含特有属性)各属性取值及含义(精准说明)属性作用及使用示例(分属性详细说明)备注(兼容性、使用规范、注意事项)
Border(边框控件)一、基础公共属性(继承自FrameworkElement):1. Name、Width、Height、Margin、HorizontalAlignment、VerticalAlignment、Visibility、Background、Opacity、RenderTransform、Effect、ToolTip2. Border特有核心属性:1. BorderBrush、BorderThickness、CornerRadius、Padding、Child1. BorderBrush:Brush类型,边框颜色,默认透明(无可见边框);2. BorderThickness:Thickness类型,边框厚度,默认0(无边框),可分别设置四个方向厚度(如“1,2,3,4”表示左上右下);3. CornerRadius:CornerRadius类型,边框圆角半径,默认0(直角),可分别设置四个角半径(如“5,5,0,0”表示左上、右上圆角,左下、右下直角);4. Padding:Thickness类型,边框内部边距,控制Child与边框的间距,默认0;5. Child:UIElement类型,边框包裹的子控件,仅能有一个直接子控件,默认null。1. 基础边框(BorderBrush + BorderThickness):作用:为控件添加边框,提升界面层次感,适用于表单输入框、面板、卡片等场景;示例:<Border BorderBrush="#E0E0E0" BorderThickness="1" Width="220" Height="40" Margin="10">&nbsp;&nbsp;<TextBox Width="200" Height="30" Margin="10" Text="带边框的文本框"/></Border>说明:Border边框颜色为浅灰色,厚度1px,内部包裹TextBox;Padding默认0,通过TextBox的Margin控制与边框的间距,实现带边框的输入框效果。2. 圆角边框(CornerRadius):示例:<Border BorderBrush="#1E90FF" BorderThickness="2" CornerRadius="8" Width="300" Height="150" Margin="10" Background="#F8F9FA" Padding="10">&nbsp;&nbsp;<TextBlock Text="圆角边框卡片" FontSize="16" FontWeight="Bold"/></Border>说明:边框厚度2px,颜色天蓝色,四个角圆角半径8px,背景色浅灰色,内部边距10px;实现圆角卡片效果,常用于突出显示重要内容。1. 兼容性:支持WPF 3.0及以上版本,无兼容性问题;2. 使用规范:- 边框可见条件:需同时设置BorderBrush(非透明)和BorderThickness(大于0),否则边框不可见;- 子控件限制:仅能有一个直接子控件,多控件需用布局控件包裹;- 圆角与背景:设置Background时,背景色会填充圆角内部区域,与边框协同实现卡片效果;- 边距控制:Padding控制子控件与边框的内部间距,Margin控制Border与其他控件的外部间距;3. 注意事项:- 性能影响:Border为轻量级控件,对性能影响极小,可大量使用;- 嵌套使用:可嵌套多个Border实现复杂边框效果(如双层边框、不同颜色边框);- 与GroupBox区别:GroupBox带有标题和边框,侧重分组;Border仅提供边框和圆角功能,无标题,用途更通用;- 自定义边框:可通过ControlTemplate修改边框的外观,如实现虚线边框、渐变边框等;- 边框方向:BorderThickness支持分别设置四个方向的厚度,如BorderThickness="1,0,1,1"表示仅显示左、右、下边框,常用于下划线效果。
Button(按钮控件)一、基础公共属性(继承自FrameworkElement):1. Name、Width、Height、Margin、Padding、HorizontalAlignment、VerticalAlignment、Visibility、IsEnabled、Background、Foreground、BorderBrush、BorderThickness、FontFamily、FontSize、FontWeight、FontStyle、ToolTip二、继承自ContentControl的属性:1. Content、ContentTemplate、ContentTemplateSelector、ContentStringFormat三、Button特有属性:1. IsDefault、IsCancel、ClickMode、Command、CommandParameter、CommandTarget、IsPressed、IsMouseOver、IsFocused、BorderBrush、BorderThickness(强化样式属性)1. IsDefault:bool类型,true=设为默认按钮(按Enter键触发),false=非默认按钮(默认);2. IsCancel:bool类型,true=设为取消按钮(按Esc键触发),false=非取消按钮(默认);3. ClickMode:枚举类型,取值为Release(鼠标释放时触发点击,默认)、Press(鼠标按下时触发)、Hover(鼠标悬停时触发);4. Command:ICommand类型,绑定命令对象,实现MVVM模式下的命令触发;5. CommandParameter:object类型,传递给Command的参数;6. CommandTarget:IInputElement类型,指定命令作用的目标元素;7. IsPressed:bool类型(只读),true=按钮被按下,false=未被按下;8. IsMouseOver:bool类型(只读),true=鼠标悬停在按钮上,false=未悬停;9. Content:object类型,按钮显示的内容(可是文本、图片等);10. 其他公共属性取值:与WPF通用属性规范一致(如Visibility取值Visible/Hidden/Collapsed等)一、核心属性作用及示例:1. IsDefault:作用=设置默认响应Enter键的按钮,提升操作效率;示例:<Button IsDefault="True" Content="确认" Click="Btn_Confirm_Click"/>(按Enter键触发Btn_Confirm_Click事件)2. IsCancel:作用=设置默认响应Esc键的按钮,快速取消操作;示例:<Button IsCancel="True" Content="取消" Click="Btn_Cancel_Click"/>(按Esc键触发Btn_Cancel_Click事件)3. ClickMode:作用=灵活控制按钮点击事件的触发时机;示例:<Button ClickMode="Press" Content="按下触发" Click="Btn_Press_Click"/>(鼠标按下瞬间触发事件)4. Command:作用=实现MVVM分离,视图与逻辑解耦;示例:<Button Content="执行命令" Command="{Binding SubmitCommand}" CommandParameter="提交数据"/>(点击时执行SubmitCommand,传递参数“提交数据”)5. Content:作用=定义按钮显示内容,支持多种内容类型;示例1(文本内容):<Button Content="文本按钮"/>示例2(图片+文本):<Button><StackPanel><Image Source="icon.png"/><TextBlock Text="图文按钮"/></StackPanel></Button>二、公共属性示例(以Background为例):作用=设置按钮背景色;示例:<Button Background="#FF007ACC" Content="蓝色按钮"/>1. 兼容性:支持WPF 3.0及以上所有版本,无兼容性问题;2. 使用规范:IsDefault和IsCancel在同一窗口中建议各只设置一个,避免按键响应冲突;3. 注意事项:- Click事件与Command可同时存在,执行顺序为Command先执行,再执行Click事件;- 当IsEnabled设为false时,按钮不可点击,Command也不会触发;- 自定义按钮样式时,需注意IsPressed、IsMouseOver等状态的视觉区分,提升交互体验;- Content为复杂对象时,建议配合ContentTemplate使用,确保显示格式正确
Calendar(日历控件)一、基础公共属性(继承自FrameworkElement):1. Name、Width、Height、Margin、HorizontalAlignment、VerticalAlignment、Visibility、IsEnabled、Background、BorderBrush、BorderThickness、Opacity、RenderTransform、Effect、ToolTip2. Calendar特有核心属性:1. SelectedDate、SelectedDates、DisplayDate、DisplayDateStart、DisplayDateEnd、FirstDayOfWeek、BlackoutDates、SelectionMode、IsTodayHighlighted1. SelectedDate:DateTime?类型,单个选中日期,默认null;2. SelectedDates:SelectedDatesCollection类型,多个选中日期,适用于多选模式;3. DisplayDate:DateTime类型,默认显示的日期(所在月份),默认当前系统日期;4. DisplayDateStart/DisplayDateEnd:DateTime类型,可显示的日期范围;5. SelectionMode:枚举类型,选择模式,SingleDate(单选,默认)、MultipleRange(多范围选择)、SingleRange(单范围选择);6. BlackoutDates:CalendarBlackoutDatesCollection类型,不可选中的日期(灰色显示);7. IsTodayHighlighted:bool类型,是否高亮显示今天,true(默认)、false(不高亮)。1. 单日期选择(默认模式):作用:选择单个日期,适用于出生日期、预约日期等场景;示例:<Calendar SelectedDate="{Binding SelectedAppointDate, Mode=TwoWay}" Width="300" Height="250" Margin="10" IsTodayHighlighted="True"/>说明:Calendar默认单选模式,SelectedDate绑定选中的预约日期;今天日期高亮显示,用户可快速识别当前日期。2. 多日期选择(SelectionMode=MultipleRange):作用:选择多个不连续或连续的日期,适用于多日期预约、日期筛选等场景;示例:<Calendar SelectionMode="MultipleRange" SelectedDates="{Binding SelectedDateRange, Mode=TwoWay}" Width="300" Height="250" Margin="10">&nbsp;&nbsp;<Calendar.BlackoutDates>&nbsp;&nbsp;&nbsp;&nbsp;<CalendarBlackoutDatesCollection>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<CalendarDateRange Start="2024/01/01" End="2024/01/03"/><!-- 禁用1-3号 -->&nbsp;&nbsp;&nbsp;&nbsp;</CalendarBlackoutDatesCollection>&nbsp;&nbsp;</Calendar.BlackoutDates></Calendar>说明:启用多范围选择模式,用户可按住Ctrl键选多个不连续日期,按住Shift键选连续日期;SelectedDates绑定选中的日期集合,同时禁用1-3号,无法选中。1. 兼容性:支持WPF 4.0及以上版本;2. 使用规范:- 选择模式匹配:单个日期用SingleDate,多个日期用MultipleRange,连续日期范围用SingleRange;- 日期范围:DisplayDateStart/DisplayDateEnd限制显示范围,BlackoutDates限制选中范围,两者可结合使用;3. 注意事项:- 绑定SelectedDates:需使用ObservableCollection<DateTime>或SelectedDatesCollection,确保选中日期变化时ViewModel同步更新;- IsEnabled设为false时:不可选择日期,整体变灰;- 导航操作:用户可通过顶部箭头切换月份/年份,也可通过代码设置DisplayDate切换;- 性能优化:大量禁用日期(BlackoutDates)时,建议批量添加日期范围,避免逐个添加影响性能;- 与DatePicker区别:Calendar显示完整日历面板,支持多日期选择;DatePicker为下拉式,侧重单日期选择,节省界面空间。
Canvas(画布控件)一、基础公共属性(继承自FrameworkElement):1. Name、Width、Height、Margin、Padding、HorizontalAlignment、VerticalAlignment、Visibility、IsEnabled、Background、BorderBrush、BorderThickness、Opacity、RenderTransform、Effect、ToolTip二、Canvas特有核心属性:1. Canvas.Left、Canvas.Top、Canvas.Right、Canvas.Bottom(附加属性,用于子控件定位)、Clip、IsItemsHost三、继承自Panel的属性:1. Children、Background、BorderBrush、BorderThickness1. Canvas.Left(附加属性):double 类型,设置子控件左上角相对于Canvas左侧的水平距离,默认0;2. Canvas.Top(附加属性):double 类型,设置子控件左上角相对于Canvas顶部的垂直距离,默认0;3. Canvas.Right(附加属性):double 类型,设置子控件右上角相对于Canvas右侧的水平距离,与Canvas.Left互斥,默认0;4. Canvas.Bottom(附加属性):double 类型,设置子控件右下角相对于Canvas底部的垂直距离,与Canvas.Top互斥,默认0;5. Children:UIElementCollection 类型,Canvas包含的子控件集合;6. Clip:Geometry 类型,设置Canvas的裁剪区域,仅显示裁剪范围内的内容;7. IsItemsHost:bool 类型,是否作为ItemsControl的项容器,true(是)、false(否,默认)。一、核心属性作用及示例:1. Canvas.Left + Canvas.Top(绝对定位):作用:实现子控件的绝对定位,适用于自定义布局、绘图、控件拖拽等场景;示例:<Canvas Width="400" Height="300" Background="#F8F8F8">&nbsp;&nbsp;<Button Content="按钮1" Canvas.Left="50" Canvas.Top="50" Width="80" Height="30"/>&nbsp;&nbsp;<TextBlock Text="绝对定位文本" Canvas.Left="150" Canvas.Top="100" Foreground="#FF4040"/>&nbsp;&nbsp;<Image Source="icon.png" Canvas.Left="250" Canvas.Top="150" Width="40" Height="40"/></Canvas>说明:三个子控件分别通过Canvas.Left和Canvas.Top设置绝对位置,不受其他控件布局影响。2. Canvas.Right + Canvas.Bottom(反向定位):作用:相对于Canvas的右侧和底部定位,适用于固定在右下角、右上角等场景;示例:<Canvas Width="400" Height="300" Background="#F8F8F8">&nbsp;&nbsp;<Button Content="右下角按钮" Canvas.Right="20" Canvas.Bottom="20" Width="100" Height="30"/>&nbsp;&nbsp;<TextBlock Text="右上角文本" Canvas.Right="20" Canvas.Top="20" Foreground="#1E90FF"/></Canvas>说明:按钮固定在Canvas右下角(距离右侧20px、底部20px),文本固定在右上角(距离右侧20px、顶部20px)。3. 子控件拖拽(结合Canvas定位):作用:实现子控件在Canvas内的自由拖拽,适用于自定义布局、绘图工具等场景;示例(拖拽TextBlock):<Canvas Width="400" Height="300" Background="#F8F8F8">&nbsp;&nbsp;<TextBlock Text="可拖拽文本" Canvas.Left="100" Canvas.Top="100" MouseDown="TextBlock_MouseDown" Foreground="#333333"/></Canvas>后台代码:private Point startPoint;private void TextBlock_MouseDown(object sender, MouseButtonEventArgs e) {&nbsp;&nbsp;var textBlock = sender as TextBlock;&nbsp;&nbsp;startPoint = e.GetPosition(canvas);&nbsp;&nbsp;textBlock.CaptureMouse();&nbsp;&nbsp;textBlock.MouseMove += TextBlock_MouseMove;&nbsp;&nbsp;textBlock.MouseUp += TextBlock_MouseUp;}private void TextBlock_MouseMove(object sender, MouseEventArgs e) {&nbsp;&nbsp;var textBlock = sender as TextBlock;&nbsp;&nbsp;if (textBlock.IsMouseCaptured) {&nbsp;&nbsp;&nbsp;&nbsp;Point currentPoint = e.GetPosition(canvas);&nbsp;&nbsp;&nbsp;&nbsp;double left = Canvas.GetLeft(textBlock) + (currentPoint.X - startPoint.X);&nbsp;&nbsp;&nbsp;&nbsp;double top = Canvas.GetTop(textBlock) + (currentPoint.Y - startPoint.Y);&nbsp;&nbsp;&nbsp;&nbsp;Canvas.SetLeft(textBlock, left);&nbsp;&nbsp;&nbsp;&nbsp;Canvas.SetTop(textBlock, top);&nbsp;&nbsp;&nbsp;&nbsp;startPoint = currentPoint;&nbsp;&nbsp;}}private void TextBlock_MouseUp(object sender, MouseButtonEventArgs e) {&nbsp;&nbsp;var textBlock = sender as TextBlock;&nbsp;&nbsp;textBlock.ReleaseMouseCapture();&nbsp;&nbsp;textBlock.MouseMove -= TextBlock_MouseMove;&nbsp;&nbsp;textBlock.MouseUp -= TextBlock_MouseUp;}说明:鼠标按下并拖动TextBlock时,TextBlock会跟随鼠标移动,实现自由拖拽。4. Clip(裁剪区域):作用:限制Canvas的显示区域,仅显示裁剪范围内的内容,适用于不规则显示区域;示例:<Canvas Width="200" Height="200" Background="#F8F8F8">&nbsp;&nbsp;<Canvas.Clip>&nbsp;&nbsp;&nbsp;&nbsp;<EllipseGeometry Center="100,100" RadiusX="100" RadiusY="100"/>&nbsp;&nbsp;</Canvas.Clip>&nbsp;&nbsp;<Image Source="large_image.jpg" Width="200" Height="200"/></Canvas>说明:Canvas的裁剪区域为圆形(圆心100,100,半径100),图片仅在圆形区域内显示,实现圆形图片效果。1. 兼容性:支持WPF 3.0及以上版本,无兼容性问题;2. 使用规范:- 定位方式:Canvas的子控件定位依赖附加属性(Left/Top/Right/Bottom),不支持自动布局,若需自适应布局,建议使用Grid、StackPanel等布局控件;- 适用场景:仅在需要绝对定位、自定义绘图、控件拖拽等场景使用Canvas,普通表单布局优先使用Grid;- 子控件层级:Canvas内子控件的层级由添加顺序决定,后添加的控件在顶层,可通过Panel.ZIndex附加属性调整层级(值越大层级越高);3. 注意事项:- Canvas的Width和Height未明确设置时,会自适应子控件的最大尺寸,可能导致超出父容器范围,建议始终设置明确的尺寸;- Left/Top与Right/Bottom互斥:同一子控件不可同时设置Left和Right(或Top和Bottom),否则仅Left/Top生效;- 性能影响:Canvas内子控件数量过多(如大量绘图元素)时,会影响渲染性能,建议使用DrawingVisual、Canvas.Clip等优化;- 无自动换行/自适应:子控件不会自动换行或调整位置,超出Canvas范围的部分会被裁剪(若未设置Clip,会显示超出部分);- 与其他布局控件嵌套:Canvas可作为其他布局控件的子控件,也可包含其他布局控件,实现混合布局(如Canvas内嵌套Grid实现局部自适应)。
CheckBox(复选框控件)一、基础公共属性(继承自FrameworkElement):1. Name、Width、Height、Margin、Padding、HorizontalAlignment、VerticalAlignment、Visibility、IsEnabled、Background、Foreground、BorderBrush、BorderThickness、FontFamily、FontSize、FontWeight、FontStyle、ToolTip二、继承自ToggleButton的属性:1. IsChecked、IsThreeState、Command、CommandParameter、CommandTarget、IsPressed、IsMouseOver、IsFocused三、CheckBox特有属性:1. Content、ContentTemplate、ContentStringFormat、HorizontalContentAlignment、VerticalContentAlignment、IsHitTestVisible1. IsChecked:bool?类型(可空布尔值),取值true=选中,false=未选中,null=不确定(仅IsThreeState为true时有效,默认false);2. IsThreeState:bool类型,true=启用三态模式(选中/未选中/不确定),false=二态模式(默认);3. Content:object类型,复选框显示的文本或内容(默认空);4. HorizontalContentAlignment:枚举类型,取值Left(左对齐,默认)、Center(居中)、Right(右对齐)、Stretch(拉伸);5. VerticalContentAlignment:枚举类型,取值Top(上对齐,默认)、Center(居中)、Bottom(下对齐)、Stretch(拉伸);6. IsHitTestVisible:bool类型,true=允许点击交互(默认),false=禁止点击;7. Command:ICommand类型,绑定命令对象,选中状态变化时触发命令一、核心属性作用及示例:1. IsChecked:作用=表示复选框的选中状态,是复选框的核心属性;示例1(二态模式):<CheckBox IsChecked="True" Content="同意协议"/>(默认选中)示例2(三态模式):<CheckBox IsThreeState="True" IsChecked="{x:Null}" Content="部分选中"/>(显示不确定状态)2. IsThreeState:作用=启用三态模式,适配部分选中场景(如父复选框对应多个子复选框,部分子复选框选中时父复选框为不确定状态);示例:<StackPanel>&nbsp;&nbsp;<CheckBox IsThreeState="True" Name="parentCb" Content="全选"/>&nbsp;&nbsp;<CheckBox Name="childCb1" Content="选项1" Checked="ChildCb_CheckedChanged" Unchecked="ChildCb_CheckedChanged"/>&nbsp;&nbsp;<CheckBox Name="childCb2" Content="选项2" Checked="ChildCb_CheckedChanged" Unchecked="ChildCb_CheckedChanged"/></StackPanel>后台代码:通过子复选框状态变化更新父复选框的IsChecked状态(全选=true,全不选=false,部分选=null)3. Content:作用=设置复选框的显示内容,支持文本或复杂对象;示例:<CheckBox><CheckBox.Content><StackPanel Orientation="Horizontal"><Image Source="check.png" Width="16" Height="16"/><TextBlock Text="带图复选框" Margin="5,0,0,0"/></StackPanel></CheckBox.Content></CheckBox>4. Command:作用=实现选中状态变化时的命令触发,适配MVVM模式;示例:<CheckBox Content="启用功能" Command="{Binding EnableFunctionCommand}" CommandParameter="{Binding IsChecked, RelativeSource={RelativeSource Self}}"/>(选中状态变化时触发命令,传递选中状态参数)二、对齐属性示例:作用=调整内容的对齐方式;示例:<CheckBox Content="居中对齐" HorizontalContentAlignment="Center" Width="100"/>(文本居中显示)1. 兼容性:支持WPF 3.0及以上版本,无兼容性问题;2. 使用规范:- 三态模式下,IsChecked的null值需通过绑定或代码设置,用户手动操作无法直接设置为null(仅能在true和false间切换);- 绑定IsChecked属性时,若为三态模式,数据源类型需为bool?(可空布尔值);3. 注意事项:- 复选框的选中状态变化会触发Checked、Unchecked、Indeterminate事件(Indeterminate仅三态模式下IsChecked变为null时触发);- IsEnabled设为false时,复选框不可点击,选中状态也无法变化;- 自定义复选框样式时,需注意三种状态(选中/未选中/不确定)的图标区分,提升用户体验;- HorizontalContentAlignment和VerticalContentAlignment仅对Content内容生效,不影响复选框本身的对齐;- 多个复选框联动时(如全选/反选),建议通过ViewModel中的逻辑实现,避免在后台代码中直接操作控件
ComboBox(组合框控件)一、基础公共属性(继承自FrameworkElement):1. Name、Width、Height、Margin、Padding、HorizontalAlignment、VerticalAlignment、Visibility、IsEnabled、Background、Foreground、BorderBrush、BorderThickness、FontFamily、FontSize、FontWeight、FontStyle、ToolTip二、继承自ItemsControl的属性:1. Items、ItemsSource、ItemTemplate、ItemTemplateSelector、DisplayMemberPath、SelectedValuePath、SelectedItem、SelectedValue、SelectedIndex三、ComboBox特有属性:1. IsDropDownOpen、IsEditable、EditableTemplate、Text、IsReadOnly、MaxDropDownHeight、DropDownWidth、DropDownHorizontalAlignment、DropDownVerticalAlignment、StaysOpenOnEdit、IsSelectionBoxHighlighted、CanUserAddItems、CanUserDeleteItems、IsSynchronizedWithCurrentItem1. IsDropDownOpen:bool类型,true=下拉列表展开,false=收起(默认);2. IsEditable:bool类型,true=允许编辑文本框内容,false=仅允许选择(默认);3. EditableTemplate:DataTemplate类型,自定义可编辑状态下的显示模板;4. Text:string类型,可编辑状态下文本框中的文本内容;5. MaxDropDownHeight:double类型,指定下拉列表的最大高度(默认无限制);6. DropDownWidth:double类型,指定下拉列表的宽度(默认与组合框宽度一致);7. StaysOpenOnEdit:bool类型,true=编辑时下拉列表保持展开,false=编辑时收起(默认);8. CanUserAddItems:bool类型,true=允许用户添加新项,false=不允许(默认);9. CanUserDeleteItems:bool类型,true=允许用户删除项,false=不允许(默认);10. IsSynchronizedWithCurrentItem:bool类型,true=选中项与ItemsSource的当前项同步,false=不同步(默认);11. SelectedIndex:int类型,获取或设置选中项的索引(-1=无选中项,默认);12. DisplayMemberPath:string类型,指定ItemsSource中用于显示的属性名;13. SelectedValuePath:string类型,指定ItemsSource中用于获取SelectedValue的属性名一、核心属性作用及示例:1. ItemsSource:作用=绑定数据源,实现数据驱动的下拉列表;示例:<ComboBox ItemsSource="{Binding UserList}" DisplayMemberPath="UserName" SelectedValuePath="UserId"/>(显示用户名,选中值为用户ID)2. IsEditable:作用=允许用户手动输入内容,提升输入灵活性;示例:<ComboBox IsEditable="True" Text="请输入或选择" ItemsSource="{Binding CityList}"/>(可输入自定义城市,也可选择列表中的城市)3. MaxDropDownHeight:作用=限制下拉列表高度,避免列表过长影响页面布局;示例:<ComboBox MaxDropDownHeight="200" ItemsSource="{Binding DataList}"/>(下拉列表最大高度200px,超出部分显示滚动条)4. CanUserAddItems:作用=允许用户添加新项到下拉列表;示例:<ComboBox CanUserAddItems="True" ItemsSource="{Binding FruitList, Mode=TwoWay}"/>(用户输入新水果名称后按Enter键可添加到列表)5. SelectedIndex:作用=通过索引设置或获取选中项;示例:<ComboBox SelectedIndex="0" ItemsSource="{Binding ColorList}"/>(默认选中第一个项)6. IsDropDownOpen:作用=手动控制下拉列表的展开/收起;示例:<Button Click="Btn_OpenDropDown_Click" Content="展开下拉列表"/>后台代码:private void Btn_OpenDropDown_Click(object sender, RoutedEventArgs e) { comboBox.IsDropDownOpen = true; }二、ItemTemplate示例(自定义下拉项显示):作用=自定义下拉项的视觉样式;示例:<ComboBox ItemsSource="{Binding ProductList}">&nbsp;&nbsp;<ComboBox.ItemTemplate>&nbsp;&nbsp;&nbsp;&nbsp;<DataTemplate>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<StackPanel Orientation="Horizontal">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<Image Source="{Binding ProductIcon}" Width="20" Height="20"/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<TextBlock Text="{Binding ProductName}" Margin="5,0,0,0"/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</StackPanel>&nbsp;&nbsp;&nbsp;&nbsp;</DataTemplate>&nbsp;&nbsp;</ComboBox.ItemTemplate></ComboBox>1. 兼容性:支持WPF 3.0及以上版本,无兼容性问题;2. 使用规范:- 绑定ItemsSource时,建议使用ObservableCollection<T>类型,确保数据源变化时UI同步更新;- DisplayMemberPath和ItemTemplate不可同时使用,若同时设置,ItemTemplate优先级更高;- CanUserAddItems和CanUserDeleteItems仅在ItemsSource为IEditableCollectionView类型时生效;3. 注意事项:- IsEditable为true时,Text属性绑定需设置Mode=TwoWay才能同步用户输入;- 下拉列表的宽度默认与组合框一致,可通过DropDownWidth手动指定不同宽度;- 当ItemsSource为空时,SelectedIndex为-1,SelectedItem和SelectedValue为null,使用时需注意空值判断;- StaysOpenOnEdit设为true时,编辑文本时下拉列表保持展开,方便用户边输入边选择;- 自定义下拉列表样式时,需注意下拉列表的边框、背景色与页面风格协调,避免遮挡其他控件
DataGrid(数据网格控件)一、基础公共属性(继承自FrameworkElement):1. Name、Width、Height、Margin、Padding、HorizontalAlignment、VerticalAlignment、Visibility、IsEnabled、Background、Foreground、BorderBrush、BorderThickness、Opacity、ToolTip二、继承自ItemsControl的属性:1. Items、ItemsSource、ItemTemplate、ItemTemplateSelector、SelectedItem、SelectedItems、SelectedIndex、IsSynchronizedWithCurrentItem三、DataGrid特有属性(核心):1. Columns、AutoGenerateColumns、CanUserAddRows、CanUserDeleteRows、CanUserResizeColumns、CanUserReorderColumns、CanUserSortColumns、IsReadOnly、SelectionMode、SelectionUnit、RowDetailsTemplate、RowDetailsVisibilityMode、HeadersVisibility、HorizontalScrollBarVisibility、VerticalScrollBarVisibility、RowHeight、HeaderHeight、AlternatingRowBackground、RowBackground、GridLinesVisibility、BorderBrush、BorderThickness1. Columns:DataGridColumnCollection类型,自定义数据网格的列集合(如DataGridTextColumn、DataGridCheckBoxColumn等);2. AutoGenerateColumns:bool类型,true=自动根据ItemsSource的数据模型生成列(默认),false=不自动生成,需手动定义Columns;3. CanUserAddRows:bool类型,true=允许用户添加新行(默认),false=禁止添加;4. CanUserDeleteRows:bool类型,true=允许用户删除行(默认),false=禁止删除;5. CanUserResizeColumns:bool类型,true=允许用户调整列宽(默认),false=禁止调整;6. CanUserReorderColumns:bool类型,true=允许用户拖动列调整顺序(默认),false=禁止拖动;7. CanUserSortColumns:bool类型,true=允许用户点击列头排序(默认),false=禁止排序;8. IsReadOnly:bool类型,true=整个网格只读(禁止编辑),false=可编辑(默认);9. SelectionMode:枚举类型,Single(单选,默认)、Extended(扩展多选,按住Ctrl/Shift键);10. SelectionUnit:枚举类型,Cell(选中单个单元格)、Row(选中整行,默认)、CellOrRowHeader(选中单元格或行头);11. RowDetailsTemplate:DataTemplate类型,自定义行详情的显示模板(展开行显示更多信息);12. RowDetailsVisibilityMode:枚举类型,行详情显示模式,Collapsed(默认折叠)、Visible(始终展开)、VisibleWhenSelected(仅选中行展开);13. HeadersVisibility:枚举类型,列头/行头显示模式,All(都显示,默认)、Column(仅显示列头)、Row(仅显示行头)、None(都不显示);14. AlternatingRowBackground:Brush类型,交替行的背景色(用于区分相邻行,提升可读性);15. GridLinesVisibility:枚举类型,网格线显示模式,All(显示所有网格线,默认)、Horizontal(仅显示水平网格线)、Vertical(仅显示垂直网格线)、None(不显示)。一、核心属性作用及示例:1. ItemsSource/Columns(数据绑定与列定义):作用:绑定数据源并自定义列显示,是DataGrid的核心功能;示例:<DataGrid ItemsSource="{Binding ProductList}" AutoGenerateColumns="False" SelectionMode="Extended">&nbsp;&nbsp;<DataGrid.Columns>&nbsp;&nbsp;&nbsp;&nbsp;<DataGridTextColumn Header="商品ID" Binding="{Binding ProductId}" IsReadOnly="True"/>&nbsp;&nbsp;&nbsp;&nbsp;<DataGridTextColumn Header="商品名称" Binding="{Binding ProductName}"/>&nbsp;&nbsp;&nbsp;&nbsp;<DataGridCheckBoxColumn Header="是否上架" Binding="{Binding IsOnSale}"/>&nbsp;&nbsp;&nbsp;&nbsp;<DataGridTemplateColumn Header="操作">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<DataGridTemplateColumn.CellTemplate>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<DataTemplate>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<Button Content="编辑" Click="Btn_EditProduct_Click"/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</DataTemplate>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</DataGridTemplateColumn.CellTemplate>&nbsp;&nbsp;&nbsp;&nbsp;</DataGridTemplateColumn>&nbsp;&nbsp;</DataGrid.Columns></DataGrid>(绑定ProductList集合,手动定义4列,支持多选)。2. AlternatingRowBackground(交替行颜色):作用:区分相邻行,提升数据网格的可读性;示例:<DataGrid ItemsSource="{Binding OrderList}" AlternatingRowBackground="#F8F9FA" RowBackground="White"/>(奇数行白色,偶数行浅灰色)。3. RowDetailsTemplate(行详情模板):作用:展开行显示更多详细信息,适用于数据字段较多的场景;示例:<DataGrid ItemsSource="{Binding UserList}" RowDetailsVisibilityMode="VisibleWhenSelected">&nbsp;&nbsp;<DataGrid.RowDetailsTemplate>&nbsp;&nbsp;&nbsp;&nbsp;<DataTemplate>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<Grid Margin="5" Background="#F5F5F5">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<TextBlock Text="{Binding UserDetail}" FontSize="12"/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</Grid>&nbsp;&nbsp;&nbsp;&nbsp;</DataTemplate>&nbsp;&nbsp;</DataGrid.RowDetailsTemplate></DataGrid>(选中行时展开显示用户详情)。4. CanUserSortColumns/CanUserResizeColumns(交互控制):作用:控制用户对列的排序、调整宽度权限;示例:<DataGrid ItemsSource="{Binding StatisticData}" CanUserSortColumns="True" CanUserResizeColumns="False"/>(允许排序,禁止调整列宽)。1. 兼容性:支持WPF 4.0及以上版本(4.0以下无此控件);2. 使用规范:- 数据绑定:ItemsSource建议绑定ObservableCollection<T>类型,确保数据源变化时UI实时同步;- 列定义:AutoGenerateColumns="False"时,需手动定义Columns,常用列类型:DataGridTextColumn(文本)、DataGridCheckBoxColumn(复选框)、DataGridComboBoxColumn(下拉框)、DataGridTemplateColumn(自定义模板);- 性能优化:数据量大(如万级以上)时,建议设置VirtualizingStackPanel.IsVirtualizing="True"启用虚拟化,减少内存占用和渲染时间;3. 注意事项:- CanUserAddRows="True"时,网格最后一行会显示“新增行”(空行),需确保ItemsSource为IEditableCollectionView类型(如ObservableCollection),否则无法添加;- IsReadOnly设为true时,整个网格不可编辑,但仍可选中、复制数据;若需单个列只读,可设置对应列的IsReadOnly="True";- RowDetailsTemplate的根元素建议设置Margin,避免与网格内容重叠;- 列宽设置:可通过DataGridColumn的Width属性设置固定宽度(如Width="100")、自动宽度(Width="Auto")或占比宽度(Width="*");- 排序功能:默认按点击列的字段升序/降序排序,若需自定义排序规则,可处理Sorting事件;- 避免在DataGrid中嵌套过多复杂控件(如大量Button、Image),否则会严重影响滚动和渲染性能。
DatePicker(日期选择器控件)一、基础公共属性(继承自FrameworkElement):1. Name、Width、Height、Margin、Padding、HorizontalAlignment、VerticalAlignment、Visibility、IsEnabled、Background、Foreground、BorderBrush、BorderThickness、FontFamily、FontSize、FontWeight、FontStyle、ToolTip二、继承自Control的属性:1. IsReadOnly、Foreground、Background(强化)三、DatePicker特有属性:1. SelectedDate、DisplayDate、DisplayDateStart、DisplayDateEnd、IsDropDownOpen、SelectedDateFormat、FirstDayOfWeek、BlackoutDates、CalendarStyle、CalendarTemplate、TextBoxStyle、Watermark、IsTodayHighlighted、IsTodayEnabled1. SelectedDate:DateTime?类型,获取或设置选中的日期(默认null,无选中日期);2. DisplayDate:DateTime类型,获取或设置日历下拉框中显示的日期(默认当前日期);3. DisplayDateStart:DateTime?类型,设置日历可显示的起始日期(默认null,无限制);4. DisplayDateEnd:DateTime?类型,设置日历可显示的结束日期(默认null,无限制);5. IsDropDownOpen:bool类型,true=日历下拉框展开,false=收起(默认);6. SelectedDateFormat:枚举类型,取值Short(短日期格式,如2024/6/1,默认)、Long(长日期格式,如2024年6月1日);7. FirstDayOfWeek:枚举类型,指定一周的第一天(取值Sunday/Monday等,默认根据系统区域设置);8. BlackoutDates:CalendarBlackoutDatesCollection类型,设置不可选择的日期集合;9. Watermark:object类型,设置文本框为空时的提示文本;10. IsTodayHighlighted:bool类型,true=高亮显示今天日期(默认),false=不高亮;11. IsTodayEnabled:bool类型,true=允许选择今天日期(默认),false=禁止选择一、核心属性作用及示例:1. SelectedDate:作用=存储用户选择的日期,是日期选择器的核心属性;示例:<DatePicker SelectedDate="{Binding ChooseDate, Mode=TwoWay}" SelectedDateFormat="Long"/>(绑定选中日期,显示长日期格式)2. DisplayDateStart/DisplayDateEnd:作用=限制日历的可显示日期范围,避免用户选择无效日期;示例:<DatePicker DisplayDateStart="2024/1/1" DisplayDateEnd="2024/12/31" Content="选择2024年日期"/>(仅能显示和选择2024年的日期)3. BlackoutDates:作用=设置不可选择的日期(如节假日、过期日期);示例:<DatePicker>&nbsp;&nbsp;<DatePicker.BlackoutDates>&nbsp;&nbsp;&nbsp;&nbsp;<CalendarBlackoutDatesCollection>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<CalendarDateRange Start="2024/5/1" End="2024/5/5"/>(5.1-5.5不可选择)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<CalendarDateRange Start="2024/10/1" End="2024/10/7"/>(10.1-10.7不可选择)&nbsp;&nbsp;&nbsp;&nbsp;</CalendarBlackoutDatesCollection>&nbsp;&nbsp;</DatePicker.BlackoutDates></DatePicker>4. Watermark:作用=显示提示文本,引导用户输入或选择;示例:<DatePicker Watermark="请选择出生日期"/>(文本框为空时显示提示)5. FirstDayOfWeek:作用=自定义一周的第一天;示例:<DatePicker FirstDayOfWeek="Monday" Content="一周第一天为周一"/>(日历中一周从周一开始显示)二、样式自定义示例:作用=自定义日历样式;示例:<DatePicker>&nbsp;&nbsp;<DatePicker.CalendarStyle>&nbsp;&nbsp;&nbsp;&nbsp;<Style TargetType="Calendar">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<Setter Property="Background" Value="#F8F9FA"/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<Setter Property="Foreground" Value="#333"/>&nbsp;&nbsp;&nbsp;&nbsp;</Style>&nbsp;&nbsp;</DatePicker.CalendarStyle></DatePicker>1. 兼容性:支持WPF 4.0及以上版本(4.0以下无此控件);2. 使用规范:- SelectedDate绑定需设置Mode=TwoWay才能同步用户选择的日期;- DisplayDateStart和DisplayDateEnd设置的范围需合理,Start不能大于End,否则无效;- BlackoutDates添加的日期范围需在DisplayDateStart和DisplayDateEnd范围内,否则不生效;3. 注意事项:- 当IsReadOnly设为true时,用户无法手动编辑文本框中的日期,但仍可通过下拉日历选择;- SelectedDateFormat的显示效果依赖系统区域设置,不同区域的短/长日期格式可能不同;- 自定义Watermark时,若设置为复杂对象,需配合WatermarkTemplate使用;- 日期选择器的文本框支持手动输入日期,输入格式需与SelectedDateFormat一致,否则无法识别;- IsTodayHighlighted设为true时,今天日期会以特殊样式显示(默认蓝色背景),便于用户快速定位今天;- 若需要禁用今天日期,可设置IsTodayEnabled="False",此时今天日期会变为不可选择状态
Expander(展开器控件)一、基础公共属性(继承自FrameworkElement):1. Name、Width、Height、Margin、Padding、HorizontalAlignment、VerticalAlignment、Visibility、IsEnabled、Background、BorderBrush、BorderThickness、Opacity、RenderTransform、Effect、ToolTip二、继承自HeaderedContentControl的核心属性:1. Header、HeaderTemplate、HeaderStringFormat、Content、ContentTemplate、ContentStringFormat、HorizontalContentAlignment、VerticalContentAlignment三、Expander特有属性:1. IsExpanded、ExpandDirection、HeaderAlignment、ExpandAnimationDuration、CollapseAnimationDuration1. IsExpanded:bool类型,核心状态属性,控制展开器的展开/折叠状态,true(展开,显示Content内容)、false(折叠,隐藏Content内容,默认);2. ExpandDirection:枚举类型,定义Content区域的展开方向,取值:Down(向下展开,默认)、Up(向上展开)、Left(向左展开)、Right(向右展开);3. Header:object类型,展开器的标题内容,支持文本、图片、复杂控件组合(如图标+文本),默认空字符串;4. HeaderAlignment:枚举类型,Header在标题栏的对齐方式,取值:Left(左对齐,默认)、Center(居中对齐)、Right(右对齐);5. HeaderTemplate:DataTemplate类型,自定义Header的显示模板,实现个性化标题样式;6. Content:object类型,展开/折叠的核心内容区域,通常为布局控件(StackPanel、Grid)包裹的交互控件集合;7. ExpandAnimationDuration/CollapseAnimationDuration:TimeSpan类型,展开/折叠时的动画时长,默认无动画(时长为0),可设置如“0:0:0.3”实现平滑过渡;8. ContentTemplate:DataTemplate类型,自定义Content区域的显示模板。一、核心属性作用及示例:1. IsExpanded + Content(基础展开/折叠功能):作用:隐藏次要内容、节省界面空间,适用于筛选条件、详细信息、附加设置等场景;示例:<Expander Header="筛选条件" IsExpanded="false" Width="350" Margin="10">&nbsp;&nbsp;<Expander.Content>&nbsp;&nbsp;&nbsp;&nbsp;<StackPanel Orientation="Vertical" Margin="5">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<StackPanel Orientation="Horizontal" Margin="0,3,0,3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<TextBlock Text="时间范围:" Width="80"/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<DatePicker Width="120" Margin="0,0,5,0"/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<TextBlock Text="至" Margin="5,0,5,0"/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<DatePicker Width="120"/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</StackPanel>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<StackPanel Orientation="Horizontal" Margin="0,3,0,3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<TextBlock Text="状态:" Width="80"/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<ComboBox Width="245" ItemsSource="{Binding StatusList}"/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</StackPanel>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<Button Content="执行筛选" Width="80" Margin="80,5,0,0"/>&nbsp;&nbsp;&nbsp;&nbsp;</StackPanel>&nbsp;&nbsp;</Expander.Content></Expander>说明:默认折叠“筛选条件”,点击标题可展开显示时间范围选择、状态选择及筛选按钮,再次点击折叠,节省界面空间。2. ExpandDirection(展开方向控制):作用:根据界面布局需求调整展开方向,适配不同空间分布;示例:<Expander Header="详细信息" ExpandDirection="Right" Width="200" Height="150" Margin="10">&nbsp;&nbsp;<TextBlock Text="用户ID:1001<LineBreak/>姓名:张三<LineBreak/>部门:技术部" Margin="5"/></Expander>说明:展开器向右展开,Content区域显示在标题栏右侧,适用于左侧空间有限、右侧有冗余空间的布局。3. 自定义Header + 动画效果:作用:提升视觉体验,实现个性化标题和平滑过渡动画;示例:<Expander Width="350" Margin="10" IsExpanded="false" ExpandAnimationDuration="0:0:0.3" CollapseAnimationDuration="0:0:0.3">&nbsp;&nbsp;<Expander.Header>&nbsp;&nbsp;&nbsp;&nbsp;<StackPanel Orientation="Horizontal">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<Image Source="icon_filter.png" Width="16" Height="16" Margin="0,0,5,0"/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<TextBlock Text="高级筛选" FontWeight="Bold" Foreground="#1E90FF"/>&nbsp;&nbsp;&nbsp;&nbsp;</StackPanel>&nbsp;&nbsp;</Expander.Header>&nbsp;&nbsp;<Expander.Content>&nbsp;&nbsp;&nbsp;&nbsp;<!-- 高级筛选内容 -->&nbsp;&nbsp;</Expander.Content></Expander>说明:Header左侧添加筛选图标,文本加粗并设为天蓝色;展开/折叠时会有0.3秒的平滑过渡动画,提升交互体验。4. 数据绑定控制展开状态:作用:通过ViewModel控制IsExpanded状态,适配MVVM模式;示例:<Expander Header="订单详情" IsExpanded="{Binding IsOrderDetailExpanded, Mode=TwoWay}" Width="400" Margin="10">&nbsp;&nbsp;<DataGrid ItemsSource="{Binding OrderItemList}" AutoGenerateColumns="False">&nbsp;&nbsp;&nbsp;&nbsp;<DataGrid.Columns>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<DataGridTextColumn Header="商品名称" Binding="{Binding ProductName}"/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<DataGridTextColumn Header="数量" Binding="{Binding Quantity}"/>&nbsp;&nbsp;&nbsp;&nbsp;</DataGrid.Columns>&nbsp;&nbsp;</DataGrid></Expander>说明:IsExpanded绑定ViewModel的IsOrderDetailExpanded属性,双向同步状态,可通过代码控制订单详情的展开/折叠(如选中订单后自动展开详情)。1. 兼容性:支持WPF 3.0及以上版本,ExpandAnimationDuration、CollapseAnimationDuration属性支持WPF 4.5及以上版本;2. 使用规范:- 内容合理性:Expander的Content应放置次要内容,核心内容建议直接显示,避免用户频繁展开/折叠影响操作效率;- 尺寸设置:展开器的Width/Height建议根据Content内容合理设置,避免展开后内容超出父容器或显示不全;- 布局适配:ExpandDirection选择需结合界面空间,向下/向上展开适用于垂直布局,向左/向右展开适用于水平布局;3. 注意事项:- 折叠状态下Content区域不渲染:折叠时Content内的控件不会被初始化和渲染,展开时才会加载,可提升初始加载性能;若需折叠时预加载Content,需自定义Expander模板;- IsEnabled设为false时:无法点击标题切换展开/折叠状态,Header文本和边框变为灰色,Content区域保持当前状态(展开/折叠);- 动画性能:设置展开/折叠动画时,避免Content内包含大量复杂控件,否则可能导致动画卡顿;- 嵌套使用:不建议多层嵌套Expander(如Expander内再放Expander),会导致界面层级混乱,影响用户操作;- 标题栏交互:默认点击Header任意位置均可切换状态,若需仅点击图标或特定区域切换,需自定义HeaderTemplate并处理点击事件。
GroupBox(分组框控件)一、基础公共属性(继承自FrameworkElement):1. Name、Width、Height、Margin、Padding、HorizontalAlignment、VerticalAlignment、Visibility、IsEnabled、Background、BorderBrush、BorderThickness、Opacity、RenderTransform、Effect、ToolTip二、继承自HeaderedContentControl的属性:1. Header、HeaderTemplate、HeaderStringFormat、Content、ContentTemplate、ContentStringFormat三、GroupBox特有属性:1. HeaderAlignment、BorderBrush、BorderThickness(强化边框样式)、Padding(内边距)1. Header:object 类型,分组框的标题内容,支持文本、图片、复杂控件组合,默认空字符串;2. HeaderTemplate:DataTemplate 类型,自定义Header的显示模板,实现个性化标题样式;3. HeaderAlignment:枚举类型,标题对齐方式,取值:Left(左对齐,默认)、Center(居中对齐)、Right(右对齐);4. Content:object 类型,分组框内部包含的内容,通常为布局控件(如StackPanel、Grid)及其他交互控件的组合;5. ContentTemplate:DataTemplate 类型,自定义Content区域的显示模板;6. BorderBrush:Brush 类型,分组框边框颜色,默认系统主题色;7. BorderThickness:Thickness 类型,分组框边框厚度,默认1px。一、核心属性作用及示例:1. Header + Content(基础分组功能):作用:将相关控件分组,提升界面逻辑性和可读性,适用于表单分区、功能模块划分等场景;示例:<GroupBox Header="用户基本信息" Width="300" Height="200" Margin="10">&nbsp;&nbsp;<StackPanel Orientation="Vertical" Margin="10">&nbsp;&nbsp;&nbsp;&nbsp;<StackPanel Orientation="Horizontal" Margin="0,5,0,5">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<TextBlock Text="姓名:" Width="60"/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<TextBox Width="150"/>&nbsp;&nbsp;&nbsp;&nbsp;</StackPanel>&nbsp;&nbsp;&nbsp;&nbsp;<StackPanel Orientation="Horizontal" Margin="0,5,0,5">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<TextBlock Text="年龄:" Width="60"/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<TextBox Width="150"/>&nbsp;&nbsp;&nbsp;&nbsp;</StackPanel>&nbsp;&nbsp;</StackPanel></GroupBox>说明:将“姓名”“年龄”相关输入控件分组,标题为“用户基本信息”,提升表单的逻辑性。2. HeaderAlignment(标题对齐):作用:调整标题的对齐方式,适配不同界面布局风格;示例:<GroupBox Header="订单信息" HeaderAlignment="Center" Width="400" Margin="10"/>说明:标题“订单信息”在分组框顶部居中对齐。3. 自定义Header模板:作用:实现个性化标题样式,如添加图标、差异化字体等;示例:<GroupBox Width="300" Height="200" Margin="10">&nbsp;&nbsp;<GroupBox.Header>&nbsp;&nbsp;&nbsp;&nbsp;<StackPanel Orientation="Horizontal">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<Image Source="icon_user.png" Width="16" Height="16" Margin="0,0,5,0"/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<TextBlock Text="用户信息" FontWeight="Bold" Foreground="#1E90FF"/>&nbsp;&nbsp;&nbsp;&nbsp;</StackPanel>&nbsp;&nbsp;</GroupBox.Header>&nbsp;&nbsp;<!-- 内部Content内容 --></GroupBox>说明:标题左侧添加用户图标,文本加粗并设置为天蓝色,提升视觉效果。4. 边框样式自定义:作用:修改分组框边框的颜色和厚度,适配界面主题;示例:<GroupBox Header="商品详情" BorderBrush="#FF6347" BorderThickness="2" Width="300" Margin="10"/>说明:分组框边框颜色设为番茄红,厚度设为2px,与默认样式区分。1. 兼容性:支持WPF 3.0及以上版本,无兼容性问题;2. 使用规范:- 分组逻辑:GroupBox的核心作用是“逻辑分组”,应将功能相关、语义关联的控件放在同一分组框内,避免无意义分组;- 内部布局:GroupBox的Content建议使用StackPanel、Grid等布局控件包裹子控件,确保内部控件排列规整;- 标题简洁:Header文本应简洁明了,准确概括分组内容,避免过长文本;3. 注意事项:- GroupBox的边框会自动环绕Content区域,设置Padding属性可调整Content与边框的间距;- IsEnabled设为false时,分组框内所有子控件会继承禁用状态,无法交互,边框和标题颜色变为灰色;- 自定义Header时,需注意Header的高度,避免过高导致遮挡Content区域;- 与Border控件的区别:GroupBox自带标题功能,Border仅提供边框,无标题,简单边框需求可优先使用Border,性能更优;- 标题位置:默认标题在分组框顶部边框的左侧,无法直接修改标题位置(如底部、左侧),若需特殊位置标题,需自定义GroupBox模板。
Image(图片控件)一、基础公共属性(继承自FrameworkElement):1. Name、Width、Height、Margin、Padding、HorizontalAlignment、VerticalAlignment、Visibility、IsEnabled、Background、BorderBrush、BorderThickness、Opacity、RenderTransform、Effect、ToolTip二、继承自Control的属性:1. Stretch、StretchDirection三、Image特有属性:1. Source、SourceUpdated、TargetUpdated、CacheOption、DecodePixelWidth、DecodePixelHeight、OpacityMask、Clip1. Source:ImageSource类型,图片的数据源,支持本地图片、网络图片、资源图片等,默认null;2. Stretch:枚举类型,图片拉伸方式,None(不拉伸,按原始尺寸显示)、Fill(拉伸填充控件,不保持比例,可能变形)、Uniform(等比例拉伸,完全包含在控件内,可能留空白,默认)、UniformToFill(等比例拉伸,填充控件,超出部分裁剪);3. StretchDirection:枚举类型,拉伸方向限制,UpOnly(仅放大,不缩小)、DownOnly(仅缩小,不放大)、Both(可放大可缩小,默认);4. CacheOption:BitmapCacheOption类型,图片缓存选项,None(不缓存)、OnLoad(加载时缓存,默认)、OnDemand(按需缓存)、CacheOnDemand(按需缓存,与OnDemand类似);5. DecodePixelWidth/DecodePixelHeight:int类型,指定图片解码时的宽度/高度,用于压缩图片,减少内存占用,默认0(按原始尺寸解码);6. OpacityMask:Brush类型,图片的透明度蒙版,用于实现不规则图片显示(如圆形图片);7. Clip:Geometry类型,图片的裁剪区域,仅显示裁剪后的部分。一、核心属性作用及示例:1. Source(图片源设置):作用:指定图片的来源,是Image控件的核心属性;示例1(本地图片):<Image Source="Images/logo.png" Width="100" Height="100"/>(Images为项目根目录下的文件夹,图片属性设为“资源”);示例2(资源图片):<Image Source="{StaticResource Icon_User}" Width="20" Height="20"/>(Icon_User为资源字典中定义的ImageSource资源);示例3(网络图片):<Image Source="https://example.com/banner.jpg" Width="500" Height="200"/>;示例4(后台设置):imageControl.Source = new BitmapImage(new Uri("Images/bg.png", UriKind.Relative));。2. Stretch/StretchDirection(拉伸控制):作用:控制图片在控件内的显示方式,避免变形或显示不全;示例1(等比例填充,裁剪超出部分):<Image Source="Images/banner.jpg" Stretch="UniformToFill" Width="500" Height="200"/>(适合 banner 图,确保填充无空白);示例2(仅缩小不放大):<Image Source="Images/icon.png" Stretch="Uniform" StretchDirection="DownOnly" Width="50" Height="50"/>(图片原始尺寸大于50×50时缩小,小于时不放大)。3. DecodePixelWidth/DecodePixelHeight(压缩解码):作用:压缩图片解码尺寸,减少内存占用,适用于大图片显示;示例:<Image Width="200" Height="200">&nbsp;&nbsp;<Image.Source>&nbsp;&nbsp;&nbsp;&nbsp;<BitmapImage UriSource="Images/large.jpg" DecodePixelWidth="200" DecodePixelHeight="200"/>&nbsp;&nbsp;</Image.Source></Image>(将大图片解码为200×200,减少内存消耗)。4. OpacityMask(透明度蒙版):作用:实现不规则图片显示(如圆形、圆角);示例(圆形图片):<Image Source="Images/avatar.jpg" Width="100" Height="100">&nbsp;&nbsp;<Image.OpacityMask>&nbsp;&nbsp;&nbsp;&nbsp;<RadialGradientBrush GradientOrigin="0.5,0.5" Center="0.5,0.5" RadiusX="0.5" RadiusY="0.5">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<GradientStop Color="Black" Offset="0"/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<GradientStop Color="Transparent" Offset="1"/>&nbsp;&nbsp;&nbsp;&nbsp;</RadialGradientBrush>&nbsp;&nbsp;</Image.OpacityMask></Image>。1. 兼容性:支持WPF 3.0及以上版本,无兼容性问题;2. 使用规范:- 图片路径:本地图片建议使用相对路径,资源图片需在项目属性中设置为“资源”或“内容”,避免路径错误;- 大图片优化:显示大图片(如几MB以上)时,务必设置DecodePixelWidth/DecodePixelHeight,否则会占用大量内存导致程序卡顿;- 网络图片:加载网络图片时建议使用异步加载(如通过HttpClient下载后再设置Source),避免阻塞UI线程;3. 注意事项:- Source为null时,Image控件显示空白,无错误提示;- Stretch="Fill"会导致图片变形,除非图片尺寸与控件尺寸比例完全一致,否则不建议使用;- CacheOption="OnLoad"会在图片加载时将整个图片缓存到内存,适用于频繁访问的图片;CacheOption="OnDemand"仅在需要显示时缓存,适用于不频繁访问的图片;- 图片格式支持:WPF默认支持BMP、JPG、PNG、GIF、ICO等常见格式,不支持WebP等特殊格式(需第三方库);- 圆角图片实现:除了OpacityMask,也可通过设置Border的CornerRadius并将Image放在Border内,设置Border.ClipToBounds="True",更简单高效;- Image控件的IsEnabled属性仅影响是否可交互(如触发事件),不影响图片显示(透明度不变)。
Label(标签控件)一、基础公共属性(继承自FrameworkElement):1. Name、Width、Height、Margin、Padding、HorizontalAlignment、VerticalAlignment、Visibility、IsEnabled、Background、Foreground、BorderBrush、BorderThickness、FontFamily、FontSize、FontWeight、FontStyle、Opacity、ToolTip二、继承自ContentControl的属性:1. Content、ContentTemplate、ContentTemplateSelector、ContentStringFormat、HorizontalContentAlignment、VerticalContentAlignment三、Label特有属性:1. Target、IsEnabled、FocusVisualStyle1. Content:object类型,标签显示的内容,支持文本、图片、复杂控件等,默认空字符串;2. ContentStringFormat:string类型,内容为数值/日期等类型时的格式化字符串(如"数值:{0:F2}"、"日期:{0:yyyy-MM-dd}"),默认空;3. HorizontalContentAlignment:枚举类型,内容水平对齐方式,Left(左对齐,默认)、Center(居中)、Right(右对齐)、Stretch(拉伸填充);4. VerticalContentAlignment:枚举类型,内容垂直对齐方式,Top(上对齐,默认)、Center(居中)、Bottom(下对齐)、Stretch(拉伸填充);5. Target:IInputElement类型,指定标签关联的输入控件(如TextBox、CheckBox),点击标签可将焦点转移到关联控件;6. FocusVisualStyle:Style类型,标签关联控件获取焦点时的视觉样式,默认系统焦点样式(通常为虚线边框);7. ContentTemplate:DataTemplate类型,自定义Content的显示模板,用于复杂内容展示。一、核心属性作用及示例:1. Content(核心显示属性):作用:展示文本提示、说明信息,或嵌套简单控件,是Label的核心功能;示例1(文本标签):<Label Content="用户名:" FontSize="14" Foreground="#333"/>示例2(嵌套图片):<Label><StackPanel Orientation="Horizontal"><Image Source="icon_user.png" Width="16" Height="16"/><TextBlock Text=" 用户信息" Margin="5,0,0,0"/></StackPanel></Label>。2. ContentStringFormat(内容格式化):作用:对非文本类型的Content进行格式化显示,避免在后台手动转换;示例:<Label Content="{Binding UserAge}" ContentStringFormat="年龄:{0}岁" FontSize="14"/>(若UserAge=25,显示为“年龄:25岁”)。3. Target(关联输入控件):作用:实现“点击标签聚焦关联控件”的交互,提升用户操作便捷性;示例:<StackPanel Orientation="Horizontal" Margin="10">&nbsp;&nbsp;<Label Content="用户名:" Target="{Binding ElementName=txtUserName}"/>&nbsp;&nbsp;<TextBox Name="txtUserName" Width="200" Margin="5,0,0,0"/></StackPanel>(点击“用户名:”标签,焦点自动定位到txtUserName文本框)。4. ContentTemplate(自定义模板):作用:自定义内容显示样式,适用于复杂内容展示;示例:<Label Content="{Binding User}">&nbsp;&nbsp;<Label.ContentTemplate>&nbsp;&nbsp;&nbsp;&nbsp;<DataTemplate>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<Grid>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<TextBlock Text="{Binding UserName}" FontWeight="Bold"/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<TextBlock Text="{Binding UserId}" FontSize="12" Foreground="#666" Margin="0,20,0,0"/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</Grid>&nbsp;&nbsp;&nbsp;&nbsp;</DataTemplate>&nbsp;&nbsp;<Label.ContentTemplate></Label>。1. 兼容性:支持WPF 3.0及以上版本,无兼容性问题;2. 使用规范:- Label与TextBlock的区别:Label支持Target关联控件、内容格式化,TextBlock更轻量(仅用于文本显示),简单文本提示优先用TextBlock,需关联输入控件时用Label;- Target属性需绑定存在的控件名称(ElementName)或数据源,否则点击标签无聚焦效果;- Content为复杂控件(如Button、TextBox)时,建议配合ContentTemplate使用,确保布局规整;3. 注意事项:- Label的Content默认支持换行(通过添加<LineBreak/>实现),如<Label Content="第一行<LineBreak/>第二行"/>;- ContentStringFormat仅对Content为值类型(int、double、DateTime等)或字符串类型生效,若Content为复杂对象,需通过ContentTemplate绑定属性显示;- 当Label的IsEnabled设为false时,Foreground颜色变为灰色,Content内容不可交互(若嵌套控件,嵌套控件也会继承禁用状态);- FocusVisualStyle仅作用于Target关联的控件,Label自身获取焦点时不生效(Label默认不可获取焦点)。
ListBox(列表框控件)一、基础公共属性(继承自FrameworkElement):1. Name、Width、Height、Margin、Padding、HorizontalAlignment、VerticalAlignment、Visibility、IsEnabled、Background、Foreground、BorderBrush、BorderThickness、FontFamily、FontSize、FontWeight、FontStyle、ToolTip二、继承自ItemsControl的属性:1. Items、ItemsSource、ItemTemplate、ItemTemplateSelector、DisplayMemberPath、SelectedValuePath、SelectedItem、SelectedValue、SelectedIndex三、ListBox特有属性:1. SelectionMode、IsSynchronizedWithCurrentItem、ScrollViewer.VerticalScrollBarVisibility、ScrollViewer.HorizontalScrollBarVisibility、SelectedItems、IsSelected、IsEnabled、Background(列表项级属性)、AlternationCount、AlternationIndex、IsTextSearchEnabled、TextSearch.TextPath、ItemContainerStyle1. SelectionMode:枚举类型,取值Single(单选,默认)、Multiple(多选,按住Ctrl键选择)、Extended(扩展多选,按住Shift/Ctrl键选择);2. SelectedItems:IList类型(只读),获取所有选中的项(仅在多选模式下有效);3. AlternationCount:int类型,指定列表项交替样式的周期数(默认0,无交替);4. AlternationIndex:int类型(只读),获取列表项在交替周期中的索引;5. IsTextSearchEnabled:bool类型,true=启用文本搜索功能(输入文本时自动定位匹配项),false=禁用(默认);6. TextSearch.TextPath:string类型,指定用于文本搜索的属性名(配合IsTextSearchEnabled使用);7. ItemContainerStyle:Style类型,自定义列表项容器(ListBoxItem)的样式;8. 其他继承属性取值:与ItemsControl通用规范一致(如SelectedIndex=-1表示无选中项)一、核心属性作用及示例:1. SelectionMode:作用=控制列表项的选择模式,适配单选/多选场景;示例:<ListBox SelectionMode="Multiple" ItemsSource="{Binding StudentList}" DisplayMemberPath="StudentName"/>(按住Ctrl键可多选学生)2. ItemsSource:作用=绑定数据源,实现数据驱动的列表显示;示例:<ListBox ItemsSource="{Binding BookList}" DisplayMemberPath="BookTitle"/>(显示书籍列表,绑定BookList集合的BookTitle属性)3. AlternationCount:作用=实现列表项交替样式,提升列表可读性;示例:<ListBox AlternationCount="2" ItemsSource="{Binding DataList}">&nbsp;&nbsp;<ListBox.ItemContainerStyle>&nbsp;&nbsp;&nbsp;&nbsp;<Style TargetType="ListBoxItem">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<Style.Triggers>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<Trigger Property="AlternationIndex" Value="0">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<Setter Property="Background" Value="#F8F9FA"/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</Trigger>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<Trigger Property="AlternationIndex" Value="1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<Setter Property="Background" Value="#FFFFFF"/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</Trigger>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</Style.Triggers>&nbsp;&nbsp;&nbsp;&nbsp;</Style>&nbsp;&nbsp;</ListBox.ItemContainerStyle></ListBox>(列表项交替显示浅灰和白色背景)4. IsTextSearchEnabled:作用=启用文本搜索,快速定位列表项;示例:<ListBox IsTextSearchEnabled="True" TextSearch.TextPath="DepartmentName" ItemsSource="{Binding DepartmentList}"/>(输入部门名称首字符可自动定位匹配项)5. SelectedItems:作用=获取多选模式下的所有选中项;示例:<Button Click="Btn_GetSelectedItems_Click" Content="获取选中项"/>后台代码:private void Btn_GetSelectedItems_Click(object sender, RoutedEventArgs e) { var selectedStudents = listBox.SelectedItems.Cast<Student>().ToList(); }二、ItemTemplate示例(自定义列表项显示):作用=展示复杂列表项内容;示例:<ListBox ItemsSource="{Binding OrderList}">&nbsp;&nbsp;<ListBox.ItemTemplate>&nbsp;&nbsp;&nbsp;&nbsp;<DataTemplate>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<Grid Margin="5">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<Grid.RowDefinitions><RowDefinition/><RowDefinition/></Grid.RowDefinitions>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<TextBlock Text="{Binding OrderId}" FontWeight="Bold"/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<TextBlock Text="{Binding OrderTime}" Grid.Row="1" FontSize="12" Foreground="#666"/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</Grid>&nbsp;&nbsp;&nbsp;&nbsp;</DataTemplate>&nbsp;&nbsp;</ListBox.ItemTemplate></ListBox>1. 兼容性:支持WPF 3.0及以上版本,无兼容性问题;2. 使用规范:- 多选模式下,建议通过SelectedItems获取选中项,避免使用SelectedItem(仅返回第一个选中项);- 启用文本搜索时,TextSearch.TextPath需指定列表项数据模型中的有效属性名;- AlternationCount设置后,需通过ItemContainerStyle的Trigger绑定AlternationIndex实现交替样式;3. 注意事项:- 列表项较多时,建议设置VirtualizingStackPanel.IsVirtualizing="True"启用虚拟化,提升性能;- 自定义ListBoxItem样式时,需注意选中、悬停、禁用等状态的视觉区分;- IsSynchronizedWithCurrentItem设为true时,SelectedItem与ItemsSource的当前项同步,适用于数据导航场景;- 列表框的滚动条默认自动显示,可通过ScrollViewer属性手动控制显示规则;- 绑定ItemsSource时,优先使用ObservableCollection<T>类型,确保数据变化时UI实时更新
Menu(菜单控件)一、基础公共属性(继承自FrameworkElement):1. Name、Width、Height、Margin、Padding、HorizontalAlignment、VerticalAlignment、Visibility、IsEnabled、Background、Foreground、BorderBrush、BorderThickness、Opacity、ToolTip二、继承自ItemsControl的属性:1. Items、ItemsSource、ItemTemplate、ItemTemplateSelector、SelectedItem、SelectedIndex三、Menu特有属性:1. IsMainMenu、ItemContainerStyle、ItemContainerStyleSelector、HorizontalContentAlignment、VerticalContentAlignment、Background、Foreground(强化菜单样式)1. IsMainMenu:bool类型,true=将菜单设为主菜单(默认),按Alt键可激活,false=普通菜单(需手动激活);2. Items:ItemCollection类型,菜单的子项集合,子项通常为MenuItem控件;3. ItemsSource:IEnumerable类型,绑定数据源生成菜单子项,适用于动态菜单;4. ItemTemplate:DataTemplate类型,自定义菜单子项的显示模板;5. ItemContainerStyle:Style类型,自定义菜单子项容器(MenuItem)的样式;6. HorizontalContentAlignment/VerticalContentAlignment:枚举类型,菜单内容的对齐方式,默认Left/Top;7. MenuItem子项核心属性:Header(菜单项文本)、Icon(菜单项图标)、Command(绑定命令)、CommandParameter(命令参数)、Items(子菜单)、IsChecked(是否选中,适用于勾选菜单)、IsEnabled(是否启用)。一、核心属性作用及示例:1. 静态菜单(手动定义Items):作用:创建固定结构的菜单(如应用程序顶部主菜单);示例:<Menu IsMainMenu="True" Background="#F8F9FA">&nbsp;&nbsp;<MenuItem Header="文件(F)">&nbsp;&nbsp;&nbsp;&nbsp;<MenuItem Header="新建(N)" Icon="{StaticResource Icon_New}" Command="{Binding NewCommand}"/>&nbsp;&nbsp;&nbsp;&nbsp;<MenuItem Header="打开(O)" Icon="{StaticResource Icon_Open}" Command="{Binding OpenCommand}"/>&nbsp;&nbsp;&nbsp;&nbsp;<Separator/>(分隔线)&nbsp;&nbsp;&nbsp;&nbsp;<MenuItem Header="退出(X)" Command="{Binding ExitCommand}"/>&nbsp;&nbsp;</MenuItem>&nbsp;&nbsp;<MenuItem Header="编辑(E)">&nbsp;&nbsp;&nbsp;&nbsp;<MenuItem Header="复制(C)" Command="ApplicationCommands.Copy"/>&nbsp;&nbsp;&nbsp;&nbsp;<MenuItem Header="粘贴(P)" Command="ApplicationCommands.Paste"/>&nbsp;&nbsp;</MenuItem></Menu>(顶部主菜单,包含“文件”“编辑”子菜单,绑定系统命令和自定义命令)。2. 动态菜单(绑定ItemsSource):作用:根据数据源动态生成菜单(如权限控制的动态菜单);示例:<Menu ItemsSource="{Binding DynamicMenuList}">&nbsp;&nbsp;<Menu.ItemTemplate>&nbsp;&nbsp;&nbsp;&nbsp;<DataTemplate>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<MenuItem Header="{Binding MenuName}" Icon="{Binding MenuIcon}" Command="{Binding MenuCommand}"/>&nbsp;&nbsp;&nbsp;&nbsp;</DataTemplate>&nbsp;&nbsp;</Menu.ItemTemplate></Menu>ViewModel:DynamicMenuList为ObservableCollection<MenuModel>,MenuModel包含MenuName、MenuIcon、MenuCommand等属性。3. 勾选菜单(IsChecked):作用:实现可勾选的菜单项(如“显示状态栏”“启用深色模式”);示例:<MenuItem Header="显示状态栏" IsChecked="{Binding IsShowStatusBar, Mode=TwoWay}"/>(勾选状态绑定ViewModel的IsShowStatusBar属性,同步显示/隐藏状态栏)。4. 子菜单嵌套:作用:创建多级菜单结构;示例:<MenuItem Header="设置">&nbsp;&nbsp;<MenuItem Header="外观">&nbsp;&nbsp;&nbsp;&nbsp;<MenuItem Header="浅色模式"/>&nbsp;&nbsp;&nbsp;&nbsp;<MenuItem Header="深色模式"/>&nbsp;&nbsp;</MenuItem>&nbsp;&nbsp;<MenuItem Header="快捷键"/></MenuItem>。1. 兼容性:支持WPF 3.0及以上版本,无兼容性问题;2. 使用规范:- 主菜单建议设置IsMainMenu="True",支持Alt键激活(如Alt+F激活“文件”菜单),符合Windows应用交互规范;- 菜单项文本中的“(F)”“(N)”等是快捷键提示,需配合InputBinding设置对应快捷键(如F对应文件菜单,N对应新建);- 动态菜单的ItemsSource建议使用ObservableCollection<T>,确保菜单动态更新;3. 注意事项:- Menu的子项必须是MenuItem控件或可转换为MenuItem的对象(通过ItemTemplate),直接放TextBlock等控件会显示异常;- 分隔线使用Separator控件,用于区分不同功能的菜单项;- 菜单项的Icon属性支持Image、Path等控件,建议图标尺寸统一(如16×16px);- 命令绑定:菜单项的Command可绑定自定义ICommand或系统内置命令(如ApplicationCommands.Copy),绑定系统命令时需确保当前焦点元素支持该命令;- 自定义Menu样式时,需注意菜单的hover(悬停)、selected(选中)状态的视觉区分,避免与页面其他控件样式冲突;- 菜单默认是水平排列的(主菜单),若需垂直菜单(如侧边菜单),可设置Orientation="Vertical"(需WPF 4.5及以上版本)。
MenuItem(菜单项控件)一、基础公共属性(继承自FrameworkElement):1. Name、Width、Height、Margin、Padding、Visibility、IsEnabled、Background、Foreground、BorderBrush、BorderThickness、FontFamily、FontSize、FontWeight、FontStyle、Opacity、ToolTip二、继承自HeaderedItemsControl的属性:1. Header、HeaderTemplate、HeaderStringFormat、Items、ItemsSource、ItemTemplate、ItemContainerStyle三、MenuItem特有属性:1. Icon、Command、CommandParameter、CommandTarget、IsChecked、IsCheckable、IsEnabled、IsSubmenuOpen、InputGestureText、ToolTip、HorizontalContentAlignment、VerticalContentAlignment1. Header:object类型,菜单项的标题内容,支持文本、图片等,默认空;2. Icon:object类型,菜单项左侧的图标,通常为Image或Path控件,默认空;3. Command:ICommand类型,绑定命令,点击菜单项时触发,适配MVVM模式;4. CommandParameter:object类型,传递给Command的参数;5. CommandTarget:IInputElement类型,指定命令作用的目标元素;6. IsChecked:bool类型,菜单项的选中状态(勾选标记),默认false;7. IsCheckable:bool类型,true=菜单项可勾选(显示勾选框),false=不可勾选(默认);8. IsSubmenuOpen:bool类型,true=子菜单展开,false=收起(默认),可手动控制子菜单显示;9. InputGestureText:string类型,显示快捷键提示文本(如“Ctrl+N”),仅用于显示,不实现快捷键功能;10. Items:ItemCollection类型,子菜单项集合,支持嵌套MenuItem实现多级菜单。一、核心属性作用及示例:1. Header/Icon(基础显示):作用:定义菜单项的文本和图标,是菜单项的基础属性;示例:<MenuItem Header="新建文件" Icon="<Image Source='icon_new.png' Width='16' Height='16'/>"></MenuItem>。2. Command/CommandParameter(命令绑定):作用:实现点击菜单项触发逻辑,解耦视图与逻辑;示例:<MenuItem Header="删除选中项" Command="{Binding DeleteSelectedCommand}" CommandParameter="{Binding SelectedItems, ElementName=dataGrid}"/>(点击时触发删除命令,传递数据网格的选中项作为参数)。3. IsCheckable/IsChecked(勾选菜单):作用:创建可勾选的菜单项,适用于开关类功能(如显示/隐藏、启用/禁用);示例:<MenuItem Header="启用自动保存" IsCheckable="True" IsChecked="{Binding IsAutoSaveEnabled, Mode=TwoWay}" InputGestureText="Ctrl+S"/>(可勾选,显示快捷键提示“Ctrl+N”)。4. InputGestureText(快捷键提示):作用:提示用户菜单项对应的快捷键,提升操作便捷性;示例:<MenuItem Header="复制" Command="ApplicationCommands.Copy" InputGestureText="Ctrl+C"/>(显示快捷键“Ctrl+C”,点击触发复制命令);注意:InputGestureText仅显示文本,需手动添加InputBinding实现快捷键功能:<Window.InputBindings>&nbsp;&nbsp;<KeyBinding Key="C" Modifiers="Ctrl" Command="ApplicationCommands.Copy"/></Window.InputBindings>。5. 子菜单嵌套:作用:创建多级菜单;示例:<MenuItem Header="格式">&nbsp;&nbsp;<MenuItem Header="字体">&nbsp;&nbsp;&nbsp;&nbsp;<MenuItem Header="宋体"/>&nbsp;&nbsp;&nbsp;&nbsp;<MenuItem Header="微软雅黑"/>&nbsp;&nbsp;</MenuItem>&nbsp;&nbsp;<MenuItem Header="字号">&nbsp;&nbsp;&nbsp;&nbsp;<MenuItem Header="12号"/>&nbsp;&nbsp;&nbsp;&nbsp;<MenuItem Header="14号">&nbsp;&nbsp;</MenuItem></MenuItem>。1. 兼容性:支持WPF 3.0及以上版本,无兼容性问题;2. 使用规范:- 快捷键实现:InputGestureText仅用于显示,需通过Window/UserControl的InputBindings或MenuItem的InputBindings添加KeyBinding/MouseBinding实现实际快捷键功能;- 勾选菜单:IsCheckable必须设为true,IsChecked才会显示勾选标记,否则IsChecked无效;- 命令优先级:MenuItem的Command优先级高于Click事件,若同时设置,先执行Command,再执行Click事件;3. 注意事项:- Icon属性的控件建议设置统一尺寸(如16×16px),避免菜单项高度不一致;- 子菜单默认在鼠标悬停时展开,可通过IsSubmenuOpen手动控制(如代码触发子菜单展开);- 菜单项的IsEnabled设为false时,文本变为灰色,无法点击,Command也不会触发;- 避免在MenuItem中嵌套过多复杂控件,否则会影响菜单展开/收起的流畅性;- 多级菜单的嵌套层级不宜过多(建议不超过3级),否则用户操作不便;- 当MenuItem的Items集合不为空时,会自动显示子菜单箭头,即使没有设置Icon或Header文本。
PasswordBox(密码框控件)一、基础公共属性(继承自FrameworkElement):1. Name、Width、Height、Margin、Padding、HorizontalAlignment、VerticalAlignment、Visibility、IsEnabled、Background、Foreground、BorderBrush、BorderThickness、FontFamily、FontSize、FontWeight、FontStyle、Opacity、ToolTip二、继承自Control的属性:1. IsReadOnly、Foreground、Background(强化样式)三、PasswordBox特有属性:1. Password、PasswordChar、MaxLength、IsPasswordRevealButtonEnabled、PasswordRevealButtonStyle、SelectionStart、SelectionLength、SelectedText、IsSelectionActive、CaretBrush、CaretIndex1. Password:string类型,密码框中的密码内容(可读可写),默认空字符串,不支持数据绑定(安全考虑);2. PasswordChar:char类型,密码的占位显示字符,默认'●'(黑色圆点),可设置为'*'、'#'等字符;3. MaxLength:int类型,允许输入的最大字符数,默认int.MaxValue(无限制),取值≥0;4. IsPasswordRevealButtonEnabled:bool类型,true=显示密码显示/隐藏切换按钮,false=不显示(默认);5. PasswordRevealButtonStyle:Style类型,自定义密码显示/隐藏按钮的样式;6. SelectionStart:int类型,选中文本的起始索引,默认0,仅在IsReadOnly=false时可设置;7. SelectionLength:int类型,选中文本的长度,默认0,设置为大于0的值可选中指定长度文本;8. SelectedText:string类型(只读),获取当前选中的密码文本;9. CaretBrush:Brush类型,设置光标颜色,默认与Foreground颜色一致;10. CaretIndex:int类型,获取或设置光标位置索引,默认0。一、核心属性作用及示例:1. Password/PasswordChar(核心密码属性):作用:存储密码内容并隐藏显示,是PasswordBox的核心功能;示例:<PasswordBox PasswordChar="*" Width="200" Height="30" PasswordChanged="PasswordBox_PasswordChanged"/>后台代码:private void PasswordBox_PasswordChanged(object sender, RoutedEventArgs e) { string password = (sender as PasswordBox).Password; // 获取密码内容 }。2. IsPasswordRevealButtonEnabled(密码显示切换):作用:允许用户手动切换密码显示/隐藏状态,提升输入体验(避免输入错误);示例:<PasswordBox IsPasswordRevealButtonEnabled="True" Width="200" Height="30" Hint="请输入密码"/>(右侧显示小眼睛按钮,点击切换密码显示)。3. MaxLength(密码长度限制):作用:限制密码输入长度,适配密码规则(如6-20位密码);示例:<PasswordBox MaxLength="20" Width="200" Height="30" ToolTip="密码长度不超过20位"/>(最多输入20个字符)。4. CaretBrush(光标颜色):作用:自定义光标颜色,提升视觉辨识度(尤其在深色背景下);示例:<PasswordBox CaretBrush="Red" Background="#F5F5F5" Width="200" Height="30"/>(红色光标,浅灰色背景)。5. 密码选中示例:作用:通过代码选中密码文本(如全选密码方便修改);示例:<Button Content="全选密码" Click="Btn_SelectAllPassword_Click" Margin="5"/>后台代码:private void Btn_SelectAllPassword_Click(object sender, RoutedEventArgs e) { pwdBox.SelectionStart = 0; pwdBox.SelectionLength = pwdBox.Password.Length; }。1. 兼容性:支持WPF 3.0及以上版本,IsPasswordRevealButtonEnabled属性支持WPF 4.5及以上版本;2. 使用规范:- 安全注意:Password属性不支持数据绑定(避免密码被绑定框架记录),需通过PasswordChanged事件或后台直接获取;- 密码验证:建议在PasswordChanged事件或提交按钮点击事件中进行密码验证(如非空、长度、复杂度校验);- MaxLength设置需符合业务密码规则,避免过短或过长(通常6-20位);3. 注意事项:- PasswordBox不支持撤销(Undo)、重做(Redo)功能,用户输入错误需手动删除重新输入;- IsReadOnly设为true时,用户无法编辑密码,但可选中、复制密码文本;若需完全禁止交互,可结合IsEnabled="false"使用;- IsPasswordRevealButtonEnabled为true时,按钮仅在用户输入密码后显示,无输入时不显示;- 自定义PasswordRevealButtonStyle时,需确保按钮的显示/隐藏状态与密码显示状态同步(通过绑定PasswordRevealButtonVisibility属性);- 避免在日志中打印Password属性值,防止密码泄露;敏感场景(如支付密码)建议对密码进行加密处理后再传输/存储。
ProgressBar(进度条控件)一、基础公共属性(继承自FrameworkElement):1. Name、Width、Height、Margin、Padding、HorizontalAlignment、VerticalAlignment、Visibility、IsEnabled、Background、BorderBrush、BorderThickness、Opacity、RenderTransform、Effect、ToolTip二、继承自RangeBase的核心属性:1. Minimum、Maximum、Value、IsIndeterminate三、ProgressBar特有属性:1. Orientation、Foreground(进度填充色)、IsDirectionReversed、ProgressTemplate1. Minimum:double类型,进度条可表示的最小值,默认值为0.0,取值无硬性限制(可设为负数,适配特殊进度场景);2. Maximum:double类型,进度条可表示的最大值,默认值为100.0,需大于Minimum,否则进度显示异常;3. Value:double类型,当前进度值,默认0.0,取值范围强制介于Minimum和Maximum之间,超出时自动截取到边界值;4. IsIndeterminate:bool类型,true=不确定进度模式(进度条动画循环滚动,不显示具体进度值),false=确定进度模式(按Value值显示具体进度,默认);5. Orientation:枚举类型,Horizontal(水平方向,默认)、Vertical(垂直方向),控制进度条的延伸方向;6. IsDirectionReversed:bool类型,true=进度填充方向反转(水平时从右向左,垂直时从下向上),false=默认方向(水平从左到右,垂直从上到下),默认false;7. ProgressTemplate:DataTemplate类型,自定义进度条的填充区域模板,用于实现个性化进度显示效果;8. Foreground:Brush类型,设置确定进度模式下进度填充部分的颜色,默认系统主题色(通常为蓝色)。一、核心属性作用及示例:1. Minimum/Maximum/Value(确定进度核心):作用:定义进度范围并显示具体进度,适用于可预估进度的场景(如文件下载、任务执行);示例:<ProgressBar Minimum="0" Maximum="100" Value="{Binding DownloadProgress, Mode=TwoWay}" Width="350" Height="20"/>后台/ViewModel:通过更新DownloadProgress的值(如从0递增到100),实时同步进度条显示,值超出0-100时自动显示为0或100。2. IsIndeterminate(不确定进度):作用:用于无法预估进度的场景(如数据加载、网络请求),通过循环动画提示用户“正在处理”;示例:<ProgressBar IsIndeterminate="True" Width="350" Height="20" Foreground="#0078D4"/>(蓝色循环滚动的进度条)。3. Orientation(方向控制):作用:根据页面布局需求调整进度条方向,垂直进度条常用于侧边栏等纵向空间;示例:<ProgressBar Orientation="Vertical" Minimum="0" Maximum="50" Value="30" Height="200" Width="20"/>(垂直方向进度条,高度200px,当前进度60%)。4. IsDirectionReversed(反向填充):作用:实现反向进度显示(如倒计时进度、剩余容量提示);示例:<ProgressBar Minimum="0" Maximum="100" Value="70" IsDirectionReversed="True" Width="350"/>(进度从右向左填充,显示30%未完成)。5. ProgressTemplate(自定义进度模板):作用:突破默认进度条样式,实现渐变、文字叠加等个性化效果;示例:<ProgressBar Minimum="0" Maximum="100" Value="85" Width="350">&nbsp;&nbsp;<ProgressBar.ProgressTemplate>&nbsp;&nbsp;&nbsp;&nbsp;<DataTemplate>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<Grid>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<Rectangle Fill="LinearGradientBrush">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<LinearGradientBrush.GradientStops>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<GradientStop Color="#28A745" Offset="0"/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<GradientStop Color="#20C997" Offset="1"/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</LinearGradientBrush.GradientStops>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</Rectangle>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<TextBlock Text="{Binding Value, StringFormat={}{0:F1}%}" HorizontalAlignment="Center" VerticalAlignment="Center" Foreground="White" FontWeight="Bold"/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</Grid>&nbsp;&nbsp;&nbsp;&nbsp;</DataTemplate>&nbsp;&nbsp;</ProgressBar.ProgressTemplate></ProgressBar>(渐变绿色进度条,叠加显示百分比文本)。1. 兼容性:支持WPF 3.0及以上所有版本,无版本兼容性问题;2. 使用规范:- 确定进度模式下,Maximum必须大于Minimum,否则Value值无法正常显示;- 绑定Value属性时需设置Mode=TwoWay(双向绑定),确保ViewModel数据与UI进度同步;- 垂直进度条必须明确设置Height(高度),水平进度条需明确设置Width(宽度),否则显示异常(过窄/过短);3. 注意事项:- IsIndeterminate为true时,Value属性失效,进度条仅显示循环动画;- IsDirectionReversed仅对确定进度模式生效,不确定模式下无影响;- 自定义ProgressTemplate时,模板根元素建议使用Grid,避免进度填充区域与文本等元素叠加异常;- 进度条IsEnabled设为false时,进度停止更新,颜色变为灰色(系统默认样式),提示用户当前不可交互;- 大量数据更新进度时(如高频循环更新Value),建议使用Dispatcher.BeginInvoke异步更新,避免阻塞UI线程导致卡顿。
RadioButton(单选按钮控件)一、基础公共属性(继承自FrameworkElement):1. Name、Width、Height、Margin、Padding、HorizontalAlignment、VerticalAlignment、Visibility、IsEnabled、Background、Foreground、BorderBrush、BorderThickness、FontFamily、FontSize、FontWeight、FontStyle、ToolTip二、继承自ToggleButton的属性:1. IsChecked、Command、CommandParameter、CommandTarget、IsPressed、IsMouseOver、IsFocused三、RadioButton特有属性:1. Content、ContentTemplate、ContentStringFormat、GroupName、HorizontalContentAlignment、VerticalContentAlignment、IsHitTestVisible1. IsChecked:bool类型,true=选中,false=未选中(默认);2. GroupName:string类型,指定单选按钮的分组名称,同一分组内只能有一个单选按钮被选中(默认空字符串,即同一父容器为一组);3. Content:object类型,单选按钮显示的文本或内容(默认空);4. HorizontalContentAlignment/VerticalContentAlignment:枚举类型,取值与CheckBox一致,控制Content的对齐方式;5. IsHitTestVisible:bool类型,true=允许点击交互(默认),false=禁止点击;6. Command:ICommand类型,选中状态变为true时触发命令(未选中时不触发)一、核心属性作用及示例:1. GroupName:作用=实现单选按钮分组,确保同一组内仅一个选中,是单选按钮的核心属性;示例:<StackPanel>&nbsp;&nbsp;<TextBlock Text="性别"/>&nbsp;&nbsp;<RadioButton GroupName="Gender" Content="男" IsChecked="True"/>(默认选中)&nbsp;&nbsp;<RadioButton GroupName="Gender" Content="女"/></StackPanel><StackPanel Margin="20,0,0,0">&nbsp;&nbsp;<TextBlock Text="学历"/>&nbsp;&nbsp;<RadioButton GroupName="Education" Content="本科"/>&nbsp;&nbsp;<RadioButton GroupName="Education" Content="硕士"/></StackPanel>(两个分组独立,互不影响)2. IsChecked:作用=表示单选按钮的选中状态;示例:<RadioButton GroupName="MaritalStatus" Content="已婚" IsChecked="{Binding IsMarried}"/>(绑定ViewModel中的IsMarried属性)3. Content:作用=设置单选按钮的显示内容;示例:<RadioButton GroupName="Payment"><StackPanel Orientation="Horizontal"><Image Source="wechat.png" Width="16" Height="16"/><TextBlock Text="微信支付" Margin="5,0,0,0"/></StackPanel></RadioButton>4. Command:作用=选中时触发命令,适配MVVM模式;示例:<RadioButton GroupName="Theme" Content="深色模式" Command="{Binding ChangeThemeCommand}" CommandParameter="Dark"/>(选中时触发切换深色模式命令)二、分组跨容器示例:作用=实现不同父容器的单选按钮同组;示例:<Grid>&nbsp;&nbsp;<Grid.ColumnDefinitions><ColumnDefinition/><ColumnDefinition/></Grid.ColumnDefinitions>&nbsp;&nbsp;<RadioButton Grid.Column="0" GroupName="Option" Content="选项A"/>&nbsp;&nbsp;<RadioButton Grid.Column="1" GroupName="Option" Content="选项B"/>(虽在不同列,同一GroupName仍为一组)</Grid>1. 兼容性:支持WPF 3.0及以上版本,无兼容性问题;2. 使用规范:- 同一组单选按钮必须设置相同的GroupName,否则会按父容器自动分组,可能导致分组混乱;- 若需要默认选中某个单选按钮,只需为该按钮设置IsChecked="True",同一组内其他按钮会自动设为false;3. 注意事项:- 单选按钮选中后,无法通过点击自身取消选中,只能通过选中同组内其他单选按钮取消;- 若同一组内无任何单选按钮设置IsChecked="True",则初始状态均为未选中,用户点击后才能选中;- 自定义单选按钮样式时,需注意选中和未选中状态的图标区分,建议与复选框样式区分开,避免用户混淆;- Command仅在单选按钮从未选中变为选中时触发,取消选中时不触发;- 跨父容器分组时,必须显式设置GroupName,否则无法实现同组单选功能
ScrollViewer(滚动视图控件)一、基础公共属性(继承自FrameworkElement):1. Name、Width、Height、Margin、Padding、HorizontalAlignment、VerticalAlignment、Visibility、IsEnabled、Background、BorderBrush、BorderThickness、Opacity、RenderTransform、Effect、ToolTip二、ScrollViewer特有核心属性:1. HorizontalScrollBarVisibility、VerticalScrollBarVisibility、ScrollViewer.HorizontalOffset、ScrollViewer.VerticalOffset、ScrollViewer.ViewportWidth、ScrollViewer.ViewportHeight、ScrollViewer.ExtentWidth、ScrollViewer.ExtentHeight、CanContentScroll、PanningMode、PanningRatio三、继承自ContentControl的属性:1. Content、ContentTemplate1. HorizontalScrollBarVisibility:枚举类型,水平滚动条显示规则,取值:Auto(内容超出宽度时显示,默认)、Visible(始终显示)、Hidden(隐藏,不显示但可通过代码滚动)、Disabled(禁用,不可滚动且不显示);2. VerticalScrollBarVisibility:枚举类型,垂直滚动条显示规则,取值与HorizontalScrollBarVisibility一致,默认Auto;3. HorizontalOffset:double 类型(只读),水平滚动偏移量(已滚动的水平距离);4. VerticalOffset:double 类型(只读),垂直滚动偏移量(已滚动的垂直距离);5. ViewportWidth/ViewportHeight:double 类型(只读),可视区域的宽度/高度;6. ExtentWidth/ExtentHeight:double 类型(只读),内容的总宽度/总高度;7. CanContentScroll:bool 类型,是否启用内容滚动(按逻辑单元滚动),true(按项滚动,适用于ItemsControl)、false(按物理像素滚动,默认);8. PanningMode:枚举类型,触摸平移模式,取值:None(禁用触摸平移,默认)、Horizontal(水平触摸平移)、Vertical(垂直触摸平移)、Both(双向触摸平移);9. PanningRatio:double 类型,触摸平移速度比例,默认1.0。一、核心属性作用及示例:1. VerticalScrollBarVisibility + HorizontalScrollBarVisibility(滚动条控制):作用:控制滚动条的显示规则,实现内容超出可视区域时的滚动功能;示例:<ScrollViewer Width="300" Height="200" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Disabled">&nbsp;&nbsp;<StackPanel Orientation="Vertical">&nbsp;&nbsp;&nbsp;&nbsp;<!-- 大量子控件,总高度超出200px -->&nbsp;&nbsp;&nbsp;&nbsp;<TextBlock Text="滚动内容1" Height="50"/>&nbsp;&nbsp;&nbsp;&nbsp;<TextBlock Text="滚动内容2" Height="50"/>&nbsp;&nbsp;&nbsp;&nbsp;<TextBlock Text="滚动内容3" Height="50"/>&nbsp;&nbsp;&nbsp;&nbsp;<TextBlock Text="滚动内容4" Height="50"/>&nbsp;&nbsp;&nbsp;&nbsp;<TextBlock Text="滚动内容5" Height="50"/>&nbsp;&nbsp;</StackPanel></ScrollViewer>说明:可视区域高度200px,内容总高度250px,垂直滚动条自动显示,水平滚动条禁用,仅可垂直滚动。2. CanContentScroll(逻辑滚动 vs 物理滚动):作用:控制滚动方式,适配不同内容类型;示例:<ScrollViewer Width="200" Height="150" CanContentScroll="True" VerticalScrollBarVisibility="Auto">&nbsp;&nbsp;<ListBox ItemsSource="{Binding DataList}">&nbsp;&nbsp;&nbsp;&nbsp;<ListBox.ItemTemplate>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<DataTemplate><TextBlock Text="{Binding}" Height="40"/></DataTemplate>&nbsp;&nbsp;&nbsp;&nbsp;</ListBox.ItemTemplate>&nbsp;&nbsp;</ListBox></ScrollViewer>说明:CanContentScroll设为true,滚动时按ListBox的项为单位滚动(每次滚动一个项),而非按像素滚动,适用于列表类控件。3. 代码控制滚动:作用:通过代码控制滚动到指定位置,适用于自动滚动、定位到指定内容等场景;示例(滚动到顶部/底部):<StackPanel>&nbsp;&nbsp;<ScrollViewer Name="scrollViewer" Width="300" Height="200" VerticalScrollBarVisibility="Auto">&nbsp;&nbsp;&nbsp;&nbsp;<!-- 滚动内容 -->&nbsp;&nbsp;</ScrollViewer>&nbsp;&nbsp;<StackPanel Orientation="Horizontal" Margin="0,10,0,0">&nbsp;&nbsp;&nbsp;&nbsp;<Button Content="滚动到顶部" Click="Btn_ScrollToTop_Click" Margin="0,0,10,0"/>&nbsp;&nbsp;&nbsp;&nbsp;<Button Content="滚动到底部" Click="Btn_ScrollToBottom_Click"/>&nbsp;&nbsp;</StackPanel></StackPanel>后台代码:private void Btn_ScrollToTop_Click(object sender, RoutedEventArgs e) { scrollViewer.ScrollToTop(); }private void Btn_ScrollToBottom_Click(object sender, RoutedEventArgs e) { scrollViewer.ScrollToBottom(); }说明:点击按钮可分别滚动到滚动视图的顶部和底部。4. 触摸平移(PanningMode):作用:支持触摸设备上的平移操作,提升触摸交互体验;示例:<ScrollViewer Width="400" Height="300" PanningMode="Both" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto">&nbsp;&nbsp;<Image Source="large_image.jpg" Width="800" Height="600"/></ScrollViewer>说明:启用双向触摸平移,在触摸设备上可拖动图片实现水平和垂直滚动。1. 兼容性:支持WPF 3.0及以上版本,PanningMode属性支持WPF 4.0及以上版本;2. 使用规范:- 滚动生效条件:ScrollViewer必须设置明确的Width和Height(固定尺寸),否则控件会自适应内容尺寸,无法触发滚动;- 内容容器选择:ScrollViewer的Content建议使用单一容器控件(如StackPanel、Grid、Canvas),避免直接放置多个子控件,确保布局稳定;- 逻辑滚动适用场景:CanContentScroll="True"仅适用于包含可滚动项的控件(如ListBox、ListView),普通控件(如Image、TextBlock)使用物理滚动(默认false)即可;3. 注意事项:- 滚动条Visibility设为Hidden时,滚动条不显示,但仍可通过代码(如ScrollToOffset、ScrollToTop)控制滚动;设为Disabled时,完全禁止滚动;- 性能优化:大量数据或复杂控件在ScrollViewer内滚动时,建议启用虚拟化(如VirtualizingStackPanel.IsVirtualizing="True"),减少内存占用和滚动卡顿;- 滚动事件:可通过ScrollChanged事件监听滚动状态变化(如偏移量、可视区域变化),实现滚动相关的联动逻辑;- 嵌套滚动:避免ScrollViewer嵌套使用(如ScrollViewer内再放ScrollViewer),会导致滚动交互混乱,影响用户体验;- 内容对齐:ScrollViewer的Content默认左上角对齐,可通过HorizontalAlignment和VerticalAlignment调整Content在可视区域的对齐方式(如居中对齐)。
Separator(分隔符控件)一、基础公共属性(继承自FrameworkElement):1. Name、Width、Height、Margin、HorizontalAlignment、VerticalAlignment、Visibility、Opacity、RenderTransform、Effect、ToolTip二、Separator特有属性:1. Background、BorderBrush、BorderThickness、Orientation1. Orientation:枚举类型,分隔符方向,Horizontal(水平分隔线,默认)、Vertical(垂直分隔线);2. Background:Brush类型,分隔符背景颜色,默认系统主题色;3. BorderBrush:Brush类型,分隔符边框颜色,默认与Background一致;4. BorderThickness:Thickness类型,分隔符厚度,默认1px。1. 水平分隔符(默认):作用:分隔水平排列的控件或内容区域,提升界面层次感,适用于表单分区、菜单分组等场景;示例:<StackPanel Margin="10">&nbsp;&nbsp;<TextBlock Text="基本信息"/>&nbsp;&nbsp;<TextBox Width="200" Height="30" Margin="0,3,0,3"/>&nbsp;&nbsp;<Separator Width="200" Background="#E0E0E0" Margin="0,5,0,5"/>&nbsp;&nbsp;<TextBlock Text="联系方式"/>&nbsp;&nbsp;<TextBox Width="200" Height="30" Margin="0,3,0,3"/></StackPanel>说明:水平分隔线将“基本信息”和“联系方式”区域分隔开,颜色设为浅灰色,界面层次清晰。2. 垂直分隔符(Orientation=Vertical):作用:分隔垂直排列的控件,适用于工具栏、侧边栏等水平布局场景;示例:<StackPanel Orientation="Horizontal" Margin="10" AlignItems="Center">&nbsp;&nbsp;<Button Content="新增" Width="80" Height="30"/>&nbsp;&nbsp;<Separator Orientation="Vertical" Height="20" Background="#E0E0E0" Margin="5,0,5,0"/>&nbsp;&nbsp;<Button Content="编辑" Width="80" Height="30"/>&nbsp;&nbsp;<Separator Orientation="Vertical" Height="20" Background="#E0E0E0" Margin="5,0,5,0"/>&nbsp;&nbsp;<Button Content="删除" Width="80" Height="30"/></StackPanel>说明:垂直分隔线分隔“新增”“编辑”“删除”三个按钮,高度设为20px,与按钮高度适配,布局整洁。1. 兼容性:支持WPF 3.0及以上版本,无兼容性问题;2. 使用规范:- 尺寸适配:水平分隔符通过Width控制长度,垂直分隔符通过Height控制长度;- 颜色搭配:分隔符颜色建议选择浅灰色等中性色,避免过于醒目影响主体内容;3. 注意事项:- 无交互功能:Separator仅用于界面分隔,无任何交互逻辑;- 布局容器适配:在StackPanel中使用时,水平分隔符默认填满容器宽度,垂直分隔符默认填满容器高度,可通过Width/Height手动限制;- 与Border区别:Separator专注于分隔线功能,样式简洁;Border侧重边框包裹,可实现更复杂的边框效果;- 自定义样式:可通过ControlTemplate修改分隔符的外观,如实现虚线、渐变分隔线等。
Slider(滑块控件)一、基础公共属性(继承自FrameworkElement):1. Name、Width、Height、Margin、Padding、HorizontalAlignment、VerticalAlignment、Visibility、IsEnabled、Background、Foreground、BorderBrush、BorderThickness、FontFamily、FontSize、FontWeight、FontStyle、ToolTip二、继承自RangeBase的属性:1. Minimum、Maximum、Value、SmallChange、LargeChange、IsSnapToTickEnabled、TickPlacement、TickFrequency、Ticks三、Slider特有属性:1. Orientation、IsMoveToPointEnabled、AutoToolTipEnabled、AutoToolTipPrecision、ThumbToolTipValueConverter、ThumbToolTipTemplate、HorizontalContentAlignment、VerticalContentAlignment1. Minimum:double类型,设置滑块的最小值(默认0.0);2. Maximum:double类型,设置滑块的最大值(默认100.0);3. Value:double类型,获取或设置滑块的当前值(默认0.0);4. SmallChange:double类型,滑块小步移动的增量(默认1.0,键盘方向键控制);5. LargeChange:double类型,滑块大步移动的增量(默认10.0,点击滑块轨道控制);6. IsSnapToTickEnabled:bool类型,true=滑块值自动对齐到刻度线,false=自由移动(默认);7. TickPlacement:枚举类型,指定刻度线的显示位置(取值None/TopLeft/BottomRight/Both,默认None);8. TickFrequency:double类型,设置刻度线的间隔(默认10.0);9. Ticks:IEnumerable<double>类型,自定义刻度线的位置;10. Orientation:枚举类型,取值Horizontal(水平滑块,默认)、Vertical(垂直滑块);11. IsMoveToPointEnabled:bool类型,true=点击滑块轨道时滑块直接移动到点击位置,false=仅按LargeChange移动(默认);12. AutoToolTipEnabled:bool类型,true=拖动滑块时显示当前值提示框,false=不显示(默认);13. AutoToolTipPrecision:int类型,设置提示框中数值的小数位数(默认0)一、核心属性作用及示例:1. Minimum/Maximum/Value:作用=定义滑块的取值范围和当前值,是滑块的核心属性;示例:<Slider Minimum="0" Maximum="100" Value="50" ValueChanged="Slider_ValueChanged"/>(滑块范围0-100,初始值50,值变化时触发事件)2. Orientation:作用=控制滑块的方向(水平/垂直);示例:<Slider Orientation="Vertical" Minimum="0" Maximum="10" Value="3" Height="200"/>(垂直滑块,高度200px)3. TickPlacement/TickFrequency:作用=显示刻度线,提升用户操作精度;示例:<Slider Minimum="0" Maximum="100" TickPlacement="Both" TickFrequency="10" IsSnapToTickEnabled="True"/>(滑块两侧显示刻度,间隔10,滑块值对齐刻度)4. AutoToolTipEnabled:作用=拖动时显示当前值提示,便于用户精准控制;示例:<Slider AutoToolTipEnabled="True" AutoToolTipPrecision="1" Minimum="0" Maximum="10" Value="5"/>(拖动时显示带1位小数的当前值)5. IsMoveToPointEnabled:作用=提升滑块操作效率,点击轨道直接定位;示例:<Slider IsMoveToPointEnabled="True" Minimum="0" Maximum="100" Value="20"/>(点击轨道任意位置,滑块直接移动到该位置)二、自定义刻度示例:作用=设置非均匀刻度;示例:<Slider Minimum="0" Maximum="100" TickPlacement="BottomRight">&nbsp;&nbsp;<Slider.Ticks>&nbsp;&nbsp;&nbsp;&nbsp;<system:Double>0</system:Double>&nbsp;&nbsp;&nbsp;&nbsp;<system:Double>25</system:Double>&nbsp;&nbsp;&nbsp;&nbsp;<system:Double>50</system:Double>&nbsp;&nbsp;&nbsp;&nbsp;<system:Double>75</system:Double>&nbsp;&nbsp;&nbsp;&nbsp;<system:Double>100</system:Double>&nbsp;&nbsp;</Slider.Ticks></Slider>(仅在0、25、50、75、100位置显示刻度)1. 兼容性:支持WPF 3.0及以上版本,无兼容性问题;2. 使用规范:- Value属性绑定需设置Mode=TwoWay才能同步用户调整的值;- TickFrequency的取值需结合Minimum和Maximum合理设置,避免刻度线过于密集或稀疏;- 垂直滑块需设置明确的Height,水平滑块需设置明确的Width,否则显示效果异常;3. 注意事项:- IsSnapToTickEnabled设为true时,Slider的Value只能取刻度线对应的数值,适用于需要精准选择固定值的场景;- AutoToolTipPrecision设置的小数位数需与Value的精度匹配,否则可能显示异常(如Value为整数时设置精度2,会显示xx.00);- 自定义滑块样式时,需注意滑块thumb(拖动块)、轨道、刻度线的样式区分,提升交互体验;- SmallChange和LargeChange的取值应根据滑块范围合理设置,范围大则增量可适当增大,范围小则增量减小;- 滑块的ValueChanged事件在拖动过程中会持续触发,若需要在拖动结束后才触发,可通过判断DragCompleted事件实现
TabControl(选项卡控件)一、基础公共属性(继承自FrameworkElement):1. Name、Width、Height、Margin、Padding、HorizontalAlignment、VerticalAlignment、Visibility、IsEnabled、Background、BorderBrush、BorderThickness、Opacity、RenderTransform、Effect、ToolTip二、继承自Selector的属性:1. SelectedItem、SelectedIndex、SelectedValue、SelectedValuePath、IsSynchronizedWithCurrentItem三、TabControl特有核心属性:1. Items、ItemsSource、ItemTemplate、ItemContainerStyle、TabStripPlacement、ContentTemplate、ContentTemplateSelector、SelectedContent、SelectedContentTemplate、IsEnabled四、TabItem子项属性(核心):1. Header、HeaderTemplate、Content、ContentTemplate、IsEnabled、IsSelected1. TabStripPlacement:枚举类型,选项卡标题栏(TabStrip)的位置,取值:Top(顶部,默认)、Bottom(底部)、Left(左侧)、Right(右侧);2. Items:ItemCollection 类型,选项卡子项集合,子项为TabItem控件;3. ItemsSource:IEnumerable 类型,绑定数据源动态生成TabItem,适配MVVM模式;4. ItemTemplate:DataTemplate 类型,自定义TabItem标题(Header)的显示模板;5. ItemContainerStyle:Style 类型,自定义TabItem的样式(如选中/未选中状态);6. SelectedItem:object 类型,当前选中的TabItem对象;7. SelectedIndex:int 类型,当前选中的TabItem索引,-1(无选中项,默认)、0(第一个项)、1(第二个项)等;8. TabItem.Header:object 类型,选项卡标题内容,支持文本、图片等;9. TabItem.Content:object 类型,选项卡对应的内容区域,支持任意控件;10. SelectedContent:object 类型(只读),当前选中TabItem的Content内容。一、核心属性作用及示例:1. 静态TabControl(手动定义TabItem):作用:实现固定选项卡切换,适用于功能分区明确的场景(如表单分页、功能模块切换);示例:<TabControl Width="400" Height="300" SelectedIndex="0">&nbsp;&nbsp;<TabItem Header="基本信息">&nbsp;&nbsp;&nbsp;&nbsp;<StackPanel Margin="10">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<TextBlock Text="姓名:"/><TextBox Width="200"/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<TextBlock Text="年龄:" Margin="0,10,0,0"/><TextBox Width="200"/>&nbsp;&nbsp;&nbsp;&nbsp;</StackPanel>&nbsp;&nbsp;</TabItem>&nbsp;&nbsp;<TabItem Header="联系方式">&nbsp;&nbsp;&nbsp;&nbsp;<StackPanel Margin="10">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<TextBlock Text="电话:"/><TextBox Width="200"/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<TextBlock Text="邮箱:" Margin="0,10,0,0"/><TextBox Width="200"/>&nbsp;&nbsp;&nbsp;&nbsp;</StackPanel>&nbsp;&nbsp;</TabItem>&nbsp;&nbsp;<TabItem Header="其他信息">&nbsp;&nbsp;&nbsp;&nbsp;<TextBlock Text="其他补充信息..." Margin="10"/>&nbsp;&nbsp;</TabItem></TabControl>说明:包含3个选项卡,默认选中第一个“基本信息”选项卡,点击不同标题切换对应内容区域。2. TabStripPlacement(标题栏位置):作用:调整选项卡标题栏的位置,适配不同界面布局需求;示例:<TabControl TabStripPlacement="Left" Width="400" Height="300">&nbsp;&nbsp;<TabItem Header="选项1"><TextBlock Text="选项1内容"/></TabItem>&nbsp;&nbsp;<TabItem Header="选项2"><TextBlock Text="选项2内容"/></TabItem></TabControl>说明:标题栏位于左侧,内容区域在右侧,适用于纵向空间充足的场景。3. 动态TabControl(绑定ItemsSource):作用:通过数据源动态生成选项卡,适用于选项卡数量不固定的场景(如动态加载的功能模块);示例:<TabControl ItemsSource="{Binding TabList}" SelectedIndex="0">&nbsp;&nbsp;<TabControl.ItemTemplate>&nbsp;&nbsp;&nbsp;&nbsp;<DataTemplate>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<StackPanel Orientation="Horizontal">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<Image Source="{Binding TabIcon}" Width="16" Height="16" Margin="0,0,5,0"/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<TextBlock Text="{Binding TabTitle}"/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</StackPanel>&nbsp;&nbsp;&nbsp;&nbsp;</DataTemplate>&nbsp;&nbsp;</TabControl.ItemTemplate>&nbsp;&nbsp;<TabControl.ContentTemplate>&nbsp;&nbsp;&nbsp;&nbsp;<DataTemplate>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<ContentControl Content="{Binding TabContent}"/>&nbsp;&nbsp;&nbsp;&nbsp;</DataTemplate>&nbsp;&nbsp;</TabControl.ContentTemplate></TabControl>ViewModel:TabList为ObservableCollection<TabModel>,TabModel包含TabIcon、TabTitle、TabContent等属性,动态生成带图标的选项卡及对应内容。4. 自定义TabItem样式:作用:区分选中/未选中状态的TabItem样式,提升视觉效果;示例:<TabControl Width="400" Height="300">&nbsp;&nbsp;<TabControl.ItemContainerStyle>&nbsp;&nbsp;&nbsp;&nbsp;<Style TargetType="TabItem">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<Setter Property="Background" Value="#F0F0F0"/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<Setter Property="Foreground" Value="#333333"/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<Style.Triggers>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<Trigger Property="IsSelected" Value="True">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<Setter Property="Background" Value="White"/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<Setter Property="Foreground" Value="#1E90FF"/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<Setter Property="BorderThickness" Value="1,1,1,0"/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</Trigger>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</Style.Triggers>&nbsp;&nbsp;&nbsp;&nbsp;</Style>&nbsp;&nbsp;</TabControl.ItemContainerStyle>&nbsp;&nbsp;<TabItem Header="选项1"><TextBlock Text="选项1内容"/></TabItem>&nbsp;&nbsp;<TabItem Header="选项2"><TextBlock Text="选项2内容"/></TabItem></TabControl>说明:选中的TabItem显示白色背景、天蓝色文本,未选中的显示灰色背景、黑色文本,视觉区分明显。1. 兼容性:支持WPF 3.0及以上版本,无兼容性问题;2. 使用规范:- 选项卡数量:建议选项卡数量控制在3-5个,数量过多会导致标题栏拥挤,影响操作;若数量较多,可考虑使用下拉菜单配合TabControl实现动态切换;- 动态选项卡:ItemsSource绑定的数据源建议使用ObservableCollection<T>,确保添加/删除选项卡时UI实时同步;- 内容区域:TabItem的Content建议使用布局控件(如StackPanel、Grid)包裹子控件,确保内容排列规整;3. 注意事项:- SelectedIndex默认值为-1(无选中项),若需默认选中某个选项卡,需手动设置SelectedIndex(如0)或SelectedItem;- TabStripPlacement设为Left/Right时,标题文本默认垂直显示,可通过旋转Transform调整为水平显示;- 禁用TabItem:设置TabItem的IsEnabled="False",该选项卡不可点击,文本变为灰色;- 性能优化:大量动态选项卡场景下,建议使用延迟加载(如仅加载当前选中选项卡的Content),避免一次性加载过多内容导致启动缓慢;- 自定义模板:若需修改TabControl的整体结构(如标题栏样式、内容区域边框),需自定义TabControl的ControlTemplate,而非仅修改ItemContainerStyle;- 选项卡切换事件:可通过SelectionChanged事件监听选项卡切换,实现切换时的初始化、数据刷新等逻辑。
TextBlock(文本块控件)一、基础公共属性(继承自FrameworkElement):1. Name、Width、Height、Margin、Padding、HorizontalAlignment、VerticalAlignment、Visibility、Opacity、RenderTransform、Effect、ToolTip二、TextBlock特有核心属性:1. Text、TextAlignment、TextWrapping、Foreground、FontFamily、FontSize、FontWeight、FontStyle、FontStretch、LineHeight、LineStackingStrategy、TextTrimming、Padding、Background1. Text:string 类型,显示的文本内容,默认空字符串;2. TextAlignment:枚举类型,文本水平对齐方式,取值:Left(左对齐,默认)、Center(居中对齐)、Right(右对齐)、Justify(两端对齐);3. TextWrapping:枚举类型,文本换行方式,取值:NoWrap(不换行,默认)、Wrap(自动换行,超出控件宽度时换行)、WrapWithOverflow(溢出时换行,优先完整单词);4. Foreground:Brush 类型,文本颜色,默认系统主题色(通常为黑色);5. FontFamily:FontFamily 类型,字体名称,如“微软雅黑”“Arial”等,默认系统默认字体;6. FontSize:double 类型,字体大小,默认12px;7. FontWeight:枚举类型,字体粗细,取值:Normal(常规,默认)、Bold(加粗)、Light(细体)等;8. FontStyle:枚举类型,字体样式,取值:Normal(常规,默认)、Italic(斜体);9. LineHeight:double 类型,文本行高,默认根据字体自动计算;10. TextTrimming:枚举类型,文本溢出裁剪方式,取值:None(不裁剪,默认)、CharacterEllipsis(字符级裁剪,末尾显示“...”)、WordEllipsis(单词级裁剪,末尾显示“...”);11. LineStackingStrategy:枚举类型,行堆叠策略,取值:MaxHeight(默认,按最大行高堆叠)、BlockLineHeight(按LineHeight指定高度堆叠)。一、核心属性作用及示例:1. Text + TextWrapping(基础文本显示与换行):作用:显示文本并控制换行,适用于各类文本说明场景;示例:<TextBlock Text="WPF(Windows Presentation Foundation)是微软推出的基于.NET Framework的用户界面框架,用于构建Windows桌面应用程序。" TextWrapping="Wrap" Width="200" Height="80"/>说明:文本超出200px宽度时自动换行,高度限制为80px,超出高度部分裁剪。2. TextAlignment(文本对齐):作用:调整文本的水平对齐方式,适配不同布局需求;示例:<TextBlock Text="居中显示文本" TextAlignment="Center" Width="200" Foreground="#FF4040"/>说明:文本在200px宽度的控件内居中对齐,文本颜色为暗红色。3. TextTrimming(溢出裁剪):作用:文本超出控件宽度时,通过裁剪并显示“...”避免布局错乱,适用于文本长度不确定的场景;示例:<TextBlock Text="这是一段超出宽度的长文本示例" TextTrimming="WordEllipsis" Width="150"/>说明:文本超出150px宽度时,按完整单词裁剪,末尾显示“...”,最终显示效果类似“这是一段超出宽度的...”。4. LineHeight(行高控制):作用:自定义文本行高,提升多行文本的可读性;示例:<TextBlock Text="第一行文本<LineBreak/>第二行文本<LineBreak/>第三行文本" LineHeight="25" FontSize="14"/>说明:设置行高为25px,多行文本间距更宽松,提升阅读体验。5. 富文本效果(-inline元素组合):作用:在单个TextBlock中实现多格式文本(不同颜色、粗细、字体);示例:<TextBlock Width="300">&nbsp;&nbsp;<Run Text="普通文本"/>&nbsp;&nbsp;<Run Text="加粗红色文本" Foreground="Red" FontWeight="Bold"/>&nbsp;&nbsp;<Run Text="斜体蓝色文本" Foreground="Blue" FontStyle="Italic"/></TextBlock>说明:通过Run元素组合,实现同一文本块内不同格式的文本显示。1. 兼容性:支持WPF 3.0及以上版本,无兼容性问题;2. 使用规范:- 轻量文本显示优先选择TextBlock,而非Label:TextBlock仅用于文本显示,无Label的Target关联等额外功能,性能更优;- 文本换行:TextWrapping="Wrap"需配合明确的Width属性使用,否则控件会自适应文本宽度,无法触发换行;- 富文本实现:单个TextBlock内的多格式文本通过Run、Bold、Italic等inline元素组合实现,无需嵌套多个TextBlock;3. 注意事项:- TextBlock默认不可编辑,仅用于文本展示,若需编辑功能需使用TextBox;- TextTrimming生效条件:TextWrapping必须设为NoWrap,且文本长度确实超出控件宽度,否则裁剪效果不生效;- 字体设置:FontFamily指定的字体需在目标系统中存在,否则会 fallback 到系统默认字体,建议使用常见字体或嵌入字体资源;- 性能优化:大量文本显示(如万级字符)时,建议设置TextOptions.TextFormattingMode="Display"提升渲染性能;- 换行符:XAML中直接输入的换行符无效,需使用<LineBreak/>标签实现换行,或在后台代码中使用Environment.NewLine。
TextBox(文本框控件)一、基础公共属性(继承自FrameworkElement):1. Name、Width、Height、Margin、Padding、HorizontalAlignment、VerticalAlignment、Visibility、IsEnabled、Background、Foreground、BorderBrush、BorderThickness、FontFamily、FontSize、FontWeight、FontStyle、ToolTip二、继承自Control的属性:1. IsReadOnly、Foreground、Background(强化)三、TextBox特有属性:1. Text、TextAlignment、TextWrapping、AcceptsReturn、AcceptsTab、MaxLength、MinLines、MaxLines、IsUndoEnabled、CaretBrush、CaretIndex、SelectionStart、SelectionLength、SelectedText、IsSelectionActive、ScrollViewer.VerticalScrollBarVisibility、ScrollViewer.HorizontalScrollBarVisibility、PasswordChar(密码框功能相关)、IsInactiveSelectionHighlightEnabled1. Text:string类型,文本框中的文本内容(默认空字符串);2. TextAlignment:枚举类型,取值Left(左对齐,默认)、Center(居中)、Right(右对齐)、Justify(两端对齐);3. TextWrapping:枚举类型,取值NoWrap(不换行,默认)、Wrap(换行)、WrapWithOverflow(溢出时换行);4. AcceptsReturn:bool类型,true=允许输入回车键换行,false=不允许(默认);5. AcceptsTab:bool类型,true=允许输入Tab键,false=不允许(默认);6. MaxLength:int类型,指定可输入的最大字符数(默认int.MaxValue,无限制);7. MinLines/MaxLines:int类型,指定文本框最小/最大显示行数(默认0,无限制);8. IsUndoEnabled:bool类型,true=启用撤销功能(默认),false=禁用;9. CaretBrush:Brush类型,设置光标颜色;10. CaretIndex:int类型,获取或设置光标位置索引;11. SelectionStart:int类型,设置或获取选中文本的起始索引;12. SelectionLength:int类型,设置或获取选中文本的长度;13. SelectedText:string类型(只读),获取选中文本内容;14. PasswordChar:char类型,设置密码占位字符(如'*'),仅当用作密码框时使用;15. ScrollViewer.VerticalScrollBarVisibility:枚举类型,取值Auto(自动显示,默认)、Visible(始终显示)、Hidden(隐藏)、Disabled(禁用)一、核心属性作用及示例:1. Text:作用=存储和显示用户输入的文本;示例:<TextBox Text="默认文本" TextChanged="TextBox_TextChanged"/>(文本变化时触发事件)2. TextWrapping:作用=控制文本超出文本框宽度时是否换行;示例:<TextBox TextWrapping="Wrap" Width="200" Height="100" Hint="输入多行文本..."/>(文本自动换行)3. AcceptsReturn:作用=允许输入多行文本,适用于备注、说明等场景;示例:<TextBox AcceptsReturn="True" TextWrapping="Wrap" MaxLines="5"/>(允许换行,最多显示5行)4. MaxLength:作用=限制用户输入的字符数,防止输入过长内容;示例:<TextBox MaxLength="11" Hint="请输入手机号"/>(仅允许输入11个字符,适配手机号输入)5. CaretBrush:作用=自定义光标颜色,提升视觉辨识度;示例:<TextBox CaretBrush="Red" Text="红色光标"/>(光标显示为红色)6. PasswordChar:作用=将文本框用作密码框,隐藏输入内容;示例:<TextBox PasswordChar="*" TextChanged="Password_TextChanged"/>(输入内容显示为*)7. ScrollViewer.VerticalScrollBarVisibility:作用=控制垂直滚动条显示规则;示例:<TextBox ScrollViewer.VerticalScrollBarVisibility="Auto" Height="80" AcceptsReturn="True"/>(文本超出高度时自动显示垂直滚动条)二、选择相关属性示例:作用=实现文本选择功能;示例:<Button Click="Btn_SelectAll_Click" Content="全选"/>后台代码:private void Btn_SelectAll_Click(object sender, RoutedEventArgs e) { txtBox.SelectionStart = 0; txtBox.SelectionLength = txtBox.Text.Length; }1. 兼容性:支持WPF 3.0及以上版本,无兼容性问题;2. 使用规范:- 用作密码框时,建议结合PasswordChar和IsReadOnly=False使用,同时避免绑定Text属性(存在安全风险),可通过后台获取Text值;- MaxLength仅限制用户输入的字符数,不限制通过代码设置的Text长度;3. 注意事项:- IsUndoEnabled启用时,会占用一定内存,对于大量文本输入场景可考虑禁用;- 文本框获取焦点时,默认选中所有文本,可通过设置CaretIndex=Text.Length取消默认选中;- 自定义文本框样式时,需注意光标高度、选中区域背景色等细节,提升输入体验;- AcceptsTab属性在AcceptsReturn为false时无效;- IsInactiveSelectionHighlightEnabled设为true时,文本框失去焦点后仍显示选中状态的高亮效果(默认false)
TimePicker(时间选择器控件)一、基础公共属性(继承自FrameworkElement):1. Name、Width、Height、Margin、Padding、HorizontalAlignment、VerticalAlignment、Visibility、IsEnabled、Background、BorderBrush、BorderThickness、Opacity、RenderTransform、Effect、ToolTip二、TimePicker特有核心属性:1. SelectedTime、DisplayTime、SelectedTimeFormat、IsEditable、IsDropDownOpen、MinuteIncrement、MaxTime、MinTime1. SelectedTime:TimeSpan?类型(可空时间跨度),核心选中时间属性,null(无选中时间,默认)、具体TimeSpan值(选中的时间,如14:30:00);2. DisplayTime:TimeSpan类型,下拉面板中默认显示的时间,默认当前系统时间;3. SelectedTimeFormat:枚举类型,时间显示格式,取值:Short(短格式,如“14:30”,默认)、Long(长格式,如“14:30:00”);4. IsEditable:bool类型,控制是否可编辑时间文本框,true(可编辑)、false(不可编辑,仅可通过下拉面板选择,默认);5. IsDropDownOpen:bool类型,控制下拉面板的显示状态,true(显示)、false(隐藏,默认);6. MinuteIncrement:int类型,分钟选择的增量,默认1(每分钟可选),可设置为5(每5分钟可选)、10(每10分钟可选)等,取值范围1-30;7. MinTime/MaxTime:TimeSpan类型,限制可选择的时间范围,仅可选择MinTime到MaxTime之间的时间。一、核心属性作用及示例:1. SelectedTime(基础时间选择):作用:选择具体时间,适用于预约时间、出发时间、提醒时间等场景;示例:<TimePicker SelectedTime="{Binding AppointmentTime, Mode=TwoWay}" Width="150" Height="30" SelectedTimeFormat="Short"/>说明:SelectedTime绑定ViewModel的AppointmentTime属性(TimeSpan?类型),双向同步选中时间;显示格式为短格式(如“14:30”),简洁易读。2. MinuteIncrement(分钟增量控制):作用:限制分钟选择的间隔,适配业务需求(如每15分钟一个预约时段);示例:<TimePicker SelectedTime="{Binding ReserveTime, Mode=TwoWay}" Width="150" Height="30" MinuteIncrement="15" SelectedTimeFormat="Short"/>说明:设置分钟增量为15,下拉面板中分钟仅可选择00、15、30、45,符合预约时段的业务需求,减少用户选择成本。3. 限制时间范围(MinTime + MaxTime):作用:约束可选择的时间,避免选择无效时间(如非工作时间);示例:<TimePicker Width="150" Height="30" MinTime="09:00:00" MaxTime="18:00:00" SelectedTimeFormat="Short"></TimePicker>说明:限制可选择时间为9:00到18:00之间,非工作时间(如8:30、18:30)无法选择,符合工作时段预约需求。4. 可编辑模式(IsEditable):作用:允许用户手动输入时间,提升操作效率;示例:<TimePicker IsEditable="True" SelectedTime="{Binding RemindTime, Mode=TwoWay}" Width="150" Height="30" SelectedTimeFormat="Short">&nbsp;&nbsp;<TimePicker.Resources>&nbsp;&nbsp;&nbsp;&nbsp;<local:TimeValidationRule x:Key="TimeValidationRule"/>&nbsp;&nbsp;</TimePicker.Resources>&nbsp;&nbsp;<TimePicker.SelectedTime>&nbsp;&nbsp;&nbsp;&nbsp;<Binding Path="RemindTime" Mode="TwoWay">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<Binding.ValidationRules>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<local:TimeValidationRule/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</Binding.ValidationRules>&nbsp;&nbsp;&nbsp;&nbsp;</Binding>&nbsp;&nbsp;</TimePicker.SelectedTime></TimePicker>说明:启用可编辑模式,用户可手动输入时间(如“10:30”);添加自定义时间验证规则(TimeValidationRule),确保输入时间有效;SelectedTime绑定RemindTime属性,同步输入的有效时间。1. 兼容性:支持WPF 4.5及以上版本(WPF 4.0及以下无此控件);2. 使用规范:- 数据绑定:SelectedTime绑定ViewModel属性时,ViewModel属性需定义为TimeSpan?类型(可空),避免无选中时间时出现异常;- 时间格式:SelectedTimeFormat仅控制显示格式,不影响SelectedTime的实际值(始终为TimeSpan类型);若需自定义格式(如“HH:mm”),需通过自定义模板实现;- 增量设置:MinuteIncrement取值需为1-30之间的整数,否则会自动取默认值1;3. 注意事项:- 可编辑模式验证:与DatePicker类似,IsEditable="True"时需添加时间验证逻辑,避免用户输入无效时间(如“25:60”“13:70”);- 系统时间格式影响:默认时间显示格式受系统区域设置影响(如12小时制/24小时制),若需固定为24小时制,需通过自定义文化(CultureInfo)实现;- 时间范围有效性:MinTime需小于MaxTime,否则时间范围限制无效,用户可选择任意时间;- 下拉面板操作:下拉面板中可通过点击小时/分钟部分切换选择,也可通过滚轮调整时间;- 与DatePicker组合:若需选择日期+时间,可将DatePicker和TimePicker组合使用,分别绑定日期和时间属性,后台合并为DateTime类型。
ToggleButton(切换按钮控件)一、基础公共属性(继承自FrameworkElement):1. Name、Width、Height、Margin、Padding、HorizontalAlignment、VerticalAlignment、Visibility、IsEnabled、Background、BorderBrush、BorderThickness、Opacity、RenderTransform、Effect、ToolTip二、继承自ButtonBase的核心属性:1. Content、ContentTemplate、ContentStringFormat、Command、CommandParameter、CommandTarget、IsPressed、IsMouseOver、IsFocused、ClickMode三、ToggleButton特有属性:1. IsChecked、IsThreeState、IsCheckedChanged、Background(状态区分)、Foreground(状态区分)1. IsChecked:bool? 类型(可空布尔值),核心状态属性,取值范围:true(选中状态)、false(未选中状态,默认)、null(不确定状态,仅IsThreeState为true时生效);2. IsThreeState:bool 类型,控制是否启用三态模式,true(启用三态:选中/未选中/不确定)、false(禁用三态,仅二态切换,默认);3. Content:object 类型,按钮显示内容,支持文本、图片、复杂控件组合等,默认空字符串;4. Command:ICommand 类型,状态切换时触发的命令,适配MVVM模式,状态变化(选中/未选中/不确定)时均会触发;5. CommandParameter:object 类型,传递给Command的参数,常用作状态标识;6. ClickMode:枚举类型,控制触发状态切换的时机,取值:Release(鼠标释放时触发,默认)、Press(鼠标按下时触发)、Hover(鼠标悬停时触发);7. IsCheckedChanged:路由事件,当IsChecked属性值发生变化时触发;8. 状态样式属性:Background、Foreground等样式属性可通过触发器区分IsChecked的不同状态,实现差异化视觉效果。一、核心属性作用及示例:1. IsChecked(二态切换核心):作用:实现“选中/未选中”的基础切换功能,适用于开关、选项标记等场景;示例:<ToggleButton IsChecked="{Binding IsDarkModeEnabled, Mode=TwoWay}" Content="深色模式" Width="100" Height="30"/>说明:绑定ViewModel中的IsDarkModeEnabled属性,双向同步状态,用户点击可切换深色模式的开启/关闭。2. IsThreeState(三态模式):作用:支持不确定状态,适用于父选项关联多个子选项的场景(如父选项“全选”,子选项部分选中时父选项为不确定);示例:<ToggleButton IsThreeState="True" IsChecked="{Binding ParentCheckState}" Content="全选" Width="80" Height="30"/>后台逻辑:当所有子选项选中时,ParentCheckState设为true;全未选中设为false;部分选中设为null,按钮显示不确定状态。3. Command + CommandParameter(MVVM状态交互):作用:状态切换时触发自定义命令,实现视图与逻辑解耦;示例:<ToggleButton Content="启用通知" Command="{Binding ToggleNoticeCommand}" CommandParameter="{Binding IsChecked, RelativeSource={RelativeSource Self}}"/>说明:状态切换时,将当前IsChecked值作为参数传递给ToggleNoticeCommand,命令中根据参数执行开启/关闭通知的逻辑。4. ClickMode(触发时机控制):作用:灵活控制状态切换的触发时机,适配不同交互需求;示例:<ToggleButton ClickMode="Press" IsChecked="{Binding IsRecording}" Content="开始录音" Width="120" Height="30"/>说明:鼠标按下瞬间即触发状态切换,无需等待释放,适用于需要快速响应的场景(如录音启动)。5. 状态差异化样式:作用:通过样式区分不同状态,提升视觉辨识度;示例:<ToggleButton Content="状态切换" Width="100" Height="30">&nbsp;&nbsp;<ToggleButton.Style>&nbsp;&nbsp;&nbsp;&nbsp;<Style TargetType="ToggleButton">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<Setter Property="Background" Value="#E6E6E6"/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<Setter Property="Foreground" Value="#333333"/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<Style.Triggers>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<Trigger Property="IsChecked" Value="True">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<Setter Property="Background" Value="#1E90FF"/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<Setter Property="Foreground" Value="White"/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</Trigger>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<Trigger Property="IsChecked" Value="{x:Null}">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<Setter Property="Background" Value="#FFA500"/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<Setter Property="Foreground" Value="White"/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</Trigger>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</Style.Triggers>&nbsp;&nbsp;&nbsp;&nbsp;</Style>&nbsp;&nbsp;</ToggleButton.Style></ToggleButton>说明:选中状态(True)显示天蓝色背景,不确定状态(Null)显示橙色背景,未选中状态显示默认灰色背景。1. 兼容性:支持WPF 3.0及以上所有版本,无版本兼容性问题;2. 使用规范:- 三态模式使用场景:仅当需要表示“不确定”状态时启用IsThreeState,普通开关场景建议使用默认二态模式,避免用户混淆;- 数据绑定:IsChecked绑定ViewModel属性时,若启用三态模式,ViewModel中对应属性需定义为bool? 类型(可空布尔值),否则无法接收null值;- 命令触发:Command在IsChecked值变化时触发,无论变化是用户操作还是代码修改导致;3. 注意事项:- 不确定状态(null)仅能通过代码设置,用户手动点击无法直接切换到null状态,只能在true和false之间切换;- IsEnabled设为false时,按钮不可交互,状态固定不变,样式默认变为灰色,无法触发Command和IsCheckedChanged事件;- 与普通Button的区别:ToggleButton点击后会保持选中状态,普通Button点击后无状态留存;- 自定义样式时,建议为IsPressed、IsMouseOver等交互状态也设置差异化样式,提升交互体验;- IsCheckedChanged事件与Command的执行顺序:先执行Command,再触发IsCheckedChanged事件,若需在事件中处理状态相关逻辑,需注意执行顺序影响。
ToolTip(提示框控件)一、基础公共属性(继承自FrameworkElement):1. Name、Width、Height、Margin、Padding、HorizontalAlignment、VerticalAlignment、Visibility、Background、BorderBrush、BorderThickness、Opacity、RenderTransform、Effect二、ToolTip特有核心属性:1. Content、ContentTemplate、Placement、PlacementTarget、HorizontalOffset、VerticalOffset、InitialShowDelay、ShowDuration、IsOpen1. Content:object类型,提示框显示的内容,支持文本、图片、复杂控件组合,默认空字符串;2. Placement:枚举类型,提示框相对于目标控件的位置,Top(上方,默认)、Bottom(下方)、Left(左侧)、Right(右侧)等;3. PlacementTarget:UIElement类型,提示框关联的目标控件,默认绑定设置ToolTip的控件;4. HorizontalOffset/VerticalOffset:double类型,提示框相对于目标控件的水平/垂直偏移量,默认0;5. InitialShowDelay:int类型,鼠标悬停后显示提示框的延迟时间(毫秒),默认1000ms;6. ShowDuration:int类型,提示框显示的持续时间(毫秒),默认5000ms;7. IsOpen:bool类型,提示框的显示状态,true(显示)、false(隐藏,默认)。1. 基础文本提示(Content):作用:鼠标悬停时显示提示信息,解释控件功能,适用于按钮、图标等控件的功能说明;示例:<Button Content="删除" Width="80" Height="30" Margin="10">&nbsp;&nbsp;<Button.ToolTip>&nbsp;&nbsp;&nbsp;&nbsp;<ToolTip Content="删除选中的数据,删除后不可恢复" InitialShowDelay="500" ShowDuration="3000"/>&nbsp;&nbsp;</Button.ToolTip></Button>说明:鼠标悬停在“删除”按钮上500ms后显示提示框,提示内容为删除功能说明,持续显示3000ms后自动隐藏,帮助用户了解功能风险。2. 复杂内容提示(图片+文本):示例:<Image Source="icon_help.png" Width="20" Height="20" Margin="10">&nbsp;&nbsp;<Image.ToolTip>&nbsp;&nbsp;&nbsp;<ToolTip Width="200" Height="80" Placement="Right" HorizontalOffset="5">&nbsp;&nbsp;&nbsp;&nbsp;<StackPanel Orientation="Vertical">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<Image Source="help_img.png" Width="40" Height="40" Margin="0,0,0,3"/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<TextBlock Text="点击图标查看详细帮助文档"/>&nbsp;&nbsp;&nbsp;&nbsp;</StackPanel>&nbsp;&nbsp;&nbsp;</ToolTip>&nbsp;&nbsp;</Image.ToolTip></Image>说明:提示框显示图片和文本组合内容,宽度200px、高度80px,位于图标右侧,水平偏移5px,展示更丰富的帮助信息。1. 兼容性:支持WPF 3.0及以上版本,无兼容性问题;2. 使用规范:- 内容简洁:提示框内容建议简洁明了,避免过长文本,影响阅读体验;- 延迟与时长:InitialShowDelay建议设置500-1000ms,避免鼠标误触显示;ShowDuration根据内容长度调整,确保用户能读完;- 位置选择:根据控件在界面中的位置选择Placement,避免提示框超出屏幕范围;3. 注意事项:- 触发方式:默认鼠标悬停触发显示,也可通过代码设置IsOpen=true手动显示;- 自动隐藏:鼠标离开目标控件或超过ShowDuration时长,提示框自动隐藏;- 无交互:默认提示框不可交互,若需添加按钮等交互控件,需设置IsHitTestVisible="True";- 性能优化:避免在大量控件上同时设置复杂内容的ToolTip,否则会影响界面加载性能;- 全局样式:可通过设置Application.Resources中的ToolTip样式,统一整个应用的提示框外观。
Viewbox(视图框控件)一、基础公共属性(继承自FrameworkElement):1. Name、Width、Height、Margin、HorizontalAlignment、VerticalAlignment、Visibility、Opacity、RenderTransform、Effect、ToolTip二、Viewbox特有核心属性:1. Child、Stretch、StretchDirection、HorizontalAlignment、VerticalAlignment1. Child:UIElement类型,Viewbox包裹的子控件(仅能有一个直接子控件),默认null;2. Stretch:枚举类型,子控件拉伸方式,None(不拉伸)、Fill(填充,可能变形)、Uniform(等比拉伸,适配Viewbox,不变形,默认)、UniformToFill(等比拉伸,填满Viewbox,超出部分裁剪);3. StretchDirection:枚举类型,拉伸方向限制,UpOnly(仅放大)、DownOnly(仅缩小)、Both(可放大缩小,默认);4. HorizontalAlignment/VerticalAlignment:枚举类型,子控件在Viewbox内的对齐方式,默认Center(居中)。1. 等比缩放控件(核心功能):作用:自动缩放子控件,适配Viewbox尺寸,保持子控件比例不变,适用于自适应界面、图表缩放等场景;示例:<Viewbox Width="300" Height="200" Stretch="Uniform" Margin="10">&nbsp;&nbsp;&lt;Grid Width="150" Height="100"&gt;<!-- 子控件原始尺寸150x100 -->&nbsp;&nbsp;&nbsp;<TextBlock Text="自适应缩放内容" FontSize="14"/>&nbsp;&nbsp;&nbsp;<Button Content="缩放按钮" Width="80" Height="30" Margin="0,30,0,0"/>&nbsp;&nbsp;</Grid></Viewbox>说明:Viewbox尺寸300x200,子Grid原始尺寸150x100;通过Uniform拉伸,子Grid等比放大2倍(适配Viewbox尺寸),内部控件也同步缩放,比例不变。2. 限制缩放方向(StretchDirection):示例:<Viewbox Width="100" Height="80" Stretch="Uniform" StretchDirection="DownOnly" Margin="10">&nbsp;&nbsp;&lt;Grid Width="150" Height="100"&gt;<!-- 原始尺寸大于Viewbox -->&nbsp;&nbsp;&nbsp;<TextBlock Text="仅缩小不放大"/>&nbsp;&nbsp;</Grid></Viewbox>说明:StretchDirection设为DownOnly,子控件仅在原始尺寸大于Viewbox时缩小适配,原始尺寸小于Viewbox时不放大,保持原尺寸居中显示。1. 兼容性:支持WPF 3.0及以上版本,无兼容性问题;2. 使用规范:- 子控件限制:Viewbox仅能有一个直接子控件,若需包裹多个控件,需用Grid、StackPanel等布局控件包裹后作为Child;- 拉伸方式选择:需保持子控件比例时用Uniform,需填满Viewbox时用UniformToFill,无需缩放时用None;- 尺寸设置:Viewbox建议设置明确的Width和Height,或绑定父容器尺寸,确保缩放目标清晰;3. 注意事项:- 文本缩放:子控件中的文本会随Viewbox同步缩放,无需额外设置字体大小;- 性能影响:子控件复杂(如大量图形、控件)时,缩放过程可能出现轻微卡顿,建议避免频繁调整Viewbox尺寸;- 与Image区别:Viewbox可缩放任意子控件(文本、按钮、布局等),Image仅缩放图片;- 对齐方式:HorizontalAlignment/VerticalAlignment控制子控件在Viewbox内的对齐,当子控件尺寸小于Viewbox时生效(如Stretch=None时)。
...全文
34 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

3

社区成员

发帖
与我相关
我的任务
社区描述
openTK、OpenGL、WebGL技术学习交流
图形渲染c#程序人生 技术论坛(原bbs) 广东省·深圳市
社区管理员
  • 亿只小灿灿
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧