掌握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

相关文章

Java利用Spire.XLS for Java自动化设置Excel的文档属性

《Java利用Spire.XLSforJava自动化设置Excel的文档属性》一个专业的Excel文件,其文档属性往往能大大提升文件的可管理性和可检索性,下面我们就来看看Java如何使用Spire... 目录Spire.XLS for Java 库介绍与安装Java 设置内置的 Excel 文档属性Java

Java实现字符串大小写转换的常用方法

《Java实现字符串大小写转换的常用方法》在Java中,字符串大小写转换是文本处理的核心操作之一,Java提供了多种灵活的方式来实现大小写转换,适用于不同场景和需求,本文将全面解析大小写转换的各种方法... 目录前言核心转换方法1.String类的基础方法2. 考虑区域设置的转换3. 字符级别的转换高级转换

Python使用Matplotlib和Seaborn绘制常用图表的技巧

《Python使用Matplotlib和Seaborn绘制常用图表的技巧》Python作为数据科学领域的明星语言,拥有强大且丰富的可视化库,其中最著名的莫过于Matplotlib和Seaborn,本篇... 目录1. 引言:数据可视化的力量2. 前置知识与环境准备2.1. 必备知识2.2. 安装所需库2.3

HTML5的input标签的`type`属性值详解和代码示例

《HTML5的input标签的`type`属性值详解和代码示例》HTML5的`input`标签提供了多种`type`属性值,用于创建不同类型的输入控件,满足用户输入的多样化需求,从文本输入、密码输入、... 目录一、引言二、文本类输入类型2.1 text2.2 password2.3 textarea(严格

input的accept属性让文件上传安全高效

《input的accept属性让文件上传安全高效》文章介绍了HTML的input文件上传`accept`属性在文件上传校验中的重要性和优势,通过使用`accept`属性,可以减少前端JavaScrip... 目录前言那个悄悄毁掉你上传体验的“常见写法”改变一切的 html 小特性:accept真正的魔法:让

C#借助Spire.XLS for .NET实现在Excel中添加文档属性

《C#借助Spire.XLSfor.NET实现在Excel中添加文档属性》在日常的数据处理和项目管理中,Excel文档扮演着举足轻重的角色,本文将深入探讨如何在C#中借助强大的第三方库Spire.... 目录为什么需要程序化添加Excel文档属性使用Spire.XLS for .NET库实现文档属性管理Sp

MyBatis配置文件中最常用的设置

《MyBatis配置文件中最常用的设置》文章主要介绍了MyBatis配置的优化方法,包括引用外部的properties配置文件、配置外置以实现环境解耦、配置文件中最常用的6个核心设置以及三种常用的Ma... 目录MyBATis配置优化mybatis的配置中引用外部的propertis配置文件⚠️ 注意事项X

一文详解Java常用包有哪些

《一文详解Java常用包有哪些》包是Java语言提供的一种确保类名唯一性的机制,是类的一种组织和管理方式、是一组功能相似或相关的类或接口的集合,:本文主要介绍Java常用包有哪些的相关资料,需要的... 目录Java.langjava.utiljava.netjava.iojava.testjava.sql

Springmvc常用的注解代码示例

《Springmvc常用的注解代码示例》本文介绍了SpringMVC中常用的控制器和请求映射注解,包括@Controller、@RequestMapping等,以及请求参数绑定注解,如@Request... 目录一、控制器与请求映射注解二、请求参数绑定注解三、其他常用注解(扩展)四、注解使用注意事项一、控制

前端Visual Studio Code安装配置教程之下载、汉化、常用组件及基本操作

《前端VisualStudioCode安装配置教程之下载、汉化、常用组件及基本操作》VisualStudioCode是微软推出的一个强大的代码编辑器,功能强大,操作简单便捷,还有着良好的用户界面,... 目录一、Visual Studio Code下载二、汉化三、常用组件1、Auto Rename Tag2