掌握WPF控件:熟练常用属性(一)

2024-01-14 00:36

本文主要是介绍掌握WPF控件:熟练常用属性(一),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

WPF布局常用控件(一)

Border

  • Border控件是一个装饰控件,用于围绕其他元素绘制边框和背景。它提供了一种简单的方式来为其他控件添加边框和背景样式,而无需自定义控件的绘制逻辑。
常用属性描述
Background用于设置背景颜色或图像。
BorderBrush用于设置边框的边框颜色
CornerRadius用于设置边框的圆角程度。
BorderThickness用于设置边框的宽度。它是一个Thickness对象,分别可以设置上、下、左、右边框的宽度。通过调整BorderThickness属性来控制Border控件边框的粗细程度。
Padding用来设置内边距 。
  • 下面写个例子
<Grid HorizontalAlignment="Center"><Grid.ColumnDefinitions><ColumnDefinition Width="300"></ColumnDefinition></Grid.ColumnDefinitions><Grid.RowDefinitions><RowDefinition></RowDefinition><RowDefinition></RowDefinition><RowDefinition></RowDefinition></Grid.RowDefinitions><Border Grid.Row="0" Grid.Column="0" Height="80"  BorderBrush="Black" BorderThickness="6"  Padding="10" ><TextBlock  Foreground="Red" TextWrapping="Wrap">边框颜色(BorderBrush)黑色,边框线(BorderThickness)同等宽度6</TextBlock></Border><Border Grid.Row="1" Grid.Column="0" Height="80" BorderBrush="SlateBlue" BorderThickness="5,10,15,20"  Padding="10" CornerRadius="15"><TextBlock  Foreground="Red" TextWrapping="Wrap">边框宽度左上右下不同(BorderThickness),边框圆角(CornerRadius)15</TextBlock></Border><Border Grid.Row="2" Grid.Column="0" Height="90" BorderBrush="SlateBlue" BorderThickness="5" Background="Green"  Padding="10" CornerRadius="15"><TextBlock  Foreground="White" TextWrapping="Wrap">边框颜色(BorderBrush)紫色,边框宽度(BorderThickness)上下左右都为5 ,背景颜色(Background)绿色 边框圆角(CornerRadius)15</TextBlock></Border></Grid>

Border

BulletDecorator

  • 用于在控件前添加符号(如项目符号)以实现列表项或标记样式的效果。
常用属性描述
Bullet用于定义显示在装饰器前面的“子弹”图形,这是一个内容属性,可以是任何 UIElement 类型的对象比如一个小圆点、图像或其他自定义形状。
ChildBulletDecorator 控件的主要内容区域,它可以放置任意类型的 UIElement,例如文本、按钮、面板等。
  • 下面写个例子
<Grid HorizontalAlignment="Center"><Grid.ColumnDefinitions><ColumnDefinition Width="300"></ColumnDefinition></Grid.ColumnDefinitions><Grid.RowDefinitions><RowDefinition></RowDefinition><RowDefinition></RowDefinition><RowDefinition></RowDefinition></Grid.RowDefinitions><BulletDecorator  Grid.Row="0" Grid.Column="0" Margin="0 20 0 0"><BulletDecorator.Bullet><Ellipse Width="10" Height="10" Fill="Black" /></BulletDecorator.Bullet><BulletDecorator.Child><TextBlock Foreground ="Purple">这是在文本前面增加一个圆点标记</TextBlock></BulletDecorator.Child></BulletDecorator><BulletDecorator  Grid.Row="1" Grid.Column="0"><BulletDecorator.Bullet><Image Width="10" Height="10" Source="1.png" /></BulletDecorator.Bullet><BulletDecorator.Child><TextBlock Foreground ="Purple">这是在文本前面增加一个图片</TextBlock></BulletDecorator.Child></BulletDecorator>
</Grid>

BulletDecorator

Button

  • 用于触发一个操作或事件。Button 控件允许用户通过单击或触摸来与应用程序进行交互。
常用属性描述
Content用于设置按钮上显示的文本或内容。
Background设置按钮的背景颜色或图像。
Foreground设置按钮上文本的颜色。
Width设置按钮的宽度。
Height设置按钮的高度。
BorderBrush设置按钮边框的颜色。
BorderThickness设置按钮边框的宽度。
Padding设置按钮内容与边框之间的空间。
FontFamily, FontSize, FontStyle, FontWeight用于设置按钮上文本的字体属性。
IsEnabled用于启用或禁用按钮的交互功能。
Click绑定到按钮的 Click 事件处理程序。
Template用于自定义按钮的外观和布局。
  • 下面写个列子
<Window.Resources><ControlTemplate x:Key="MyButtonTemplate" TargetType="Button"><BulletDecorator VerticalAlignment="Center"><BulletDecorator.Bullet><Ellipse Fill="LightGray" Stroke="Red" StrokeThickness="1"  Width="20" Height="20"/></BulletDecorator.Bullet><TextBlock Text="{TemplateBinding Content}" Background="Aquamarine" FontSize="20" HorizontalAlignment="Left" VerticalAlignment="Center"/></BulletDecorator></ControlTemplate>
</Window.Resources>
<Grid HorizontalAlignment="Center"><Grid.ColumnDefinitions><ColumnDefinition></ColumnDefinition></Grid.ColumnDefinitions><Grid.RowDefinitions><RowDefinition></RowDefinition><RowDefinition></RowDefinition><RowDefinition></RowDefinition><RowDefinition></RowDefinition><RowDefinition></RowDefinition><RowDefinition></RowDefinition><RowDefinition></RowDefinition><RowDefinition></RowDefinition></Grid.RowDefinitions><Button Grid.Column="0" Grid.Row="0" Height="40" Width="200" Content="设置字体颜色"     Foreground="Blue"  ></Button><Button Grid.Column="0" Grid.Row="1"  Height="40" Width="200" BorderBrush="Black" Content="设置边框颜色"></Button><Button Grid.Column="0" Grid.Row="2"  Height="40" Width="200" BorderBrush="AntiqueWhite" BorderThickness="6"  Content="设置边框颜色"></Button><Button Grid.Column="0" Grid.Row="3"  Height="40" Width="200" Background="Green" Foreground="White"  Content="设置背景颜色"></Button><Button Grid.Column="0" Grid.Row="4"  Height="40" Width="200"   Content="我被禁用了" IsEnabled="False" ></Button><Button Grid.Column="0" Grid.Row="5"  Height="40" Width="200"   Content="我绑定了点击事件" Click="Button_Click" ></Button><Button Grid.Column="0" Grid.Row="6"  Height="40" Width="200"  FontFamily="Gabriola" FontWeight="Bold" FontSize="20" FontStyle="Italic" Content="设置了字体相关属性"></Button><Button Grid.Column="0" Grid.Row="7" Template="{StaticResource MyButtonTemplate}" Content="我使用了模板"></Button>
</Grid>

Button

公众号“点滴分享技术猿


关注

这篇关于掌握WPF控件:熟练常用属性(一)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/603344

相关文章

Linux线程之线程的创建、属性、回收、退出、取消方式

《Linux线程之线程的创建、属性、回收、退出、取消方式》文章总结了线程管理核心知识:线程号唯一、创建方式、属性设置(如分离状态与栈大小)、回收机制(join/detach)、退出方法(返回/pthr... 目录1. 线程号2. 线程的创建3. 线程属性4. 线程的回收5. 线程的退出6. 线程的取消7.

全面掌握 SQL 中的 DATEDIFF函数及用法最佳实践

《全面掌握SQL中的DATEDIFF函数及用法最佳实践》本文解析DATEDIFF在不同数据库中的差异,强调其边界计算原理,探讨应用场景及陷阱,推荐根据需求选择TIMESTAMPDIFF或inte... 目录1. 核心概念:DATEDIFF 究竟在计算什么?2. 主流数据库中的 DATEDIFF 实现2.1

Spring Boot中WebSocket常用使用方法详解

《SpringBoot中WebSocket常用使用方法详解》本文从WebSocket的基础概念出发,详细介绍了SpringBoot集成WebSocket的步骤,并重点讲解了常用的使用方法,包括简单消... 目录一、WebSocket基础概念1.1 什么是WebSocket1.2 WebSocket与HTTP

golang中reflect包的常用方法

《golang中reflect包的常用方法》Go反射reflect包提供类型和值方法,用于获取类型信息、访问字段、调用方法等,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值... 目录reflect包方法总结类型 (Type) 方法值 (Value) 方法reflect包方法总结

C# 比较两个list 之间元素差异的常用方法

《C#比较两个list之间元素差异的常用方法》:本文主要介绍C#比较两个list之间元素差异,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录1. 使用Except方法2. 使用Except的逆操作3. 使用LINQ的Join,GroupJoin

python常用的正则表达式及作用

《python常用的正则表达式及作用》正则表达式是处理字符串的强大工具,Python通过re模块提供正则表达式支持,本文给大家介绍python常用的正则表达式及作用详解,感兴趣的朋友跟随小编一起看看吧... 目录python常用正则表达式及作用基本匹配模式常用正则表达式示例常用量词边界匹配分组和捕获常用re

python删除xml中的w:ascii属性的步骤

《python删除xml中的w:ascii属性的步骤》使用xml.etree.ElementTree删除WordXML中w:ascii属性,需注册命名空间并定位rFonts元素,通过del操作删除属... 可以使用python的XML.etree.ElementTree模块通过以下步骤删除XML中的w:as

Python打印对象所有属性和值的方法小结

《Python打印对象所有属性和值的方法小结》在Python开发过程中,调试代码时经常需要查看对象的当前状态,也就是对象的所有属性和对应的值,然而,Python并没有像PHP的print_r那样直接提... 目录python中打印对象所有属性和值的方法实现步骤1. 使用vars()和pprint()2. 使

gitlab安装及邮箱配置和常用使用方式

《gitlab安装及邮箱配置和常用使用方式》:本文主要介绍gitlab安装及邮箱配置和常用使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1.安装GitLab2.配置GitLab邮件服务3.GitLab的账号注册邮箱验证及其分组4.gitlab分支和标签的

Python常用命令提示符使用方法详解

《Python常用命令提示符使用方法详解》在学习python的过程中,我们需要用到命令提示符(CMD)进行环境的配置,:本文主要介绍Python常用命令提示符使用方法的相关资料,文中通过代码介绍的... 目录一、python环境基础命令【Windows】1、检查Python是否安装2、 查看Python的安