C#_WPF_按钮模板及自定义控件的使用
zhezhongyun 2025-05-03 17:51 22 浏览
源码私信联系
WPF功能强大,但是控件的用法与Winfrom不大一样。这个文件主要说明了Button控件的用法。希望能给大家一个启示。
1、按钮加入图片
<Button Grid.Row="0" Grid.Column="0" ToolTip="Set_Label" Name="set_label1" Height="45" VerticalAlignment="Stretch" Width="45" HorizontalAlignment="Stretch">
<Image Source="Resources/Player1.png" StretchDirection="DownOnly" Stretch="Fill" />
</Button>
图片的处理:
- 准备素材
- 打开Properties\Resources.resx,添加
- 素材属性的生成操作:Resources
- 引用:<Image Source="Resources/Player1.png" />
2、窗体的xaml文件中直接使用模板
<Button Grid.Row="0" Grid.Column="1" x:Name="m_HelpButton" IsEnabled="True" Width="50" Height="50">
<Button.Template>
<ControlTemplate>
<Border BorderBrush="Aqua" Background="#FFEDF961">
<Grid>
<Image Margin="2" Source="Resources/Player1.png" />
</Grid>
</Border>
</ControlTemplate>
</Button.Template>
</Button>
3、使用样式
<Button Grid.Row="0" Grid.Column="2" Content="播放" Height="31" Name="button1" Foreground="#FFDE4747" Focusable="True" BorderBrush="{x:Null}" BorderThickness="0" Style="{StaticResource ButtonLeft}" Margin="106,47,82,61"/>
App.xaml中定义样式
<Style x:Key="ButtonLeft" TargetType="{x:Type Button}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<StackPanel Orientation="Horizontal">
<Image Name="minBtn" Source="Resources\player0.png" />
<TextBlock Text="{TemplateBinding Content}" VerticalAlignment="Center" FontSize="24"></TextBlock>
</StackPanel>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Source" Value="Resources\player1.png" TargetName="minBtn" />
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter Property="Source" Value="Resources\pause0.png" TargetName="minBtn" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
4、鼠标覆盖变淡
<Button Name="Add" Margin="2"
Grid.Row="1" Grid.Column="0"
Style="{StaticResource Button_Menu}" Background="#FFEE2424" BorderBrush="#FF0031FF">
<Image Source="/Resources/pause0.png" />
</Button>
样式:
<Style x:Key="Button_Menu" TargetType="{x:Type Button}">
<Setter Property="Width" Value="50" />
<Setter Property="Height" Value="50" />
<Setter Property="BorderThickness" Value="0" />
<Setter Property="Foreground" Value="Black"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border x:Name="MyBackgroundElement" BorderBrush="{DynamicResource ForgroundBrush}" BorderThickness="0">
<ContentPresenter x:Name="ButtonContentPresenter" VerticalAlignment="Center" HorizontalAlignment="Center"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="MyBackgroundElement" Property="Background" Value="{DynamicResource ForgroundBrush100}"/>
<Setter TargetName="MyBackgroundElement" Property="Opacity" Value="0.7"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="Cursor" Value="Hand" />
</Style>
5、三态按钮及阴影效果
<Button Grid.Row="1" Grid.Column="1" Content="button" Height="50" Width="50" RenderTransformOrigin="-4.5,-1.24">
<Button.Template>
<ControlTemplate TargetType="{x:Type Button}">
<Border BorderBrush="{TemplateBinding Control.BorderBrush}" BorderThickness="0" CornerRadius="3,13,3,13" Name="PART_Background">
<Border.Background>
<LinearGradientBrush EndPoint="0,1" StartPoint="0,0">
<GradientStop Color="White" Offset="0.0" />
<GradientStop Color="Silver" Offset="0.5" />
<GradientStop Color="White" Offset="0.0" />
</LinearGradientBrush>
</Border.Background>
<ContentPresenter Content="{TemplateBinding ContentControl.Content}" HorizontalAlignment="Center" VerticalAlignment="Center" />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="UIElement.IsMouseOver" Value="True">
<Setter Property="Border.Background" TargetName="PART_Background">
<Setter.Value>
<LinearGradientBrush EndPoint="0,1" StartPoint="0,0">
<GradientStop Color="Silver" Offset="0.0" />
<GradientStop Color="White" Offset="0.5" />
<GradientStop Color="Silver" Offset="0.0" />
</LinearGradientBrush>
</Setter.Value>
</Setter>
</Trigger>
<Trigger Property="ButtonBase.IsPressed" Value="True">
<Setter Property="UIElement.Effect">
<Setter.Value>
<DropShadowEffect BlurRadius="10" Color="Black" Direction="0" Opacity="0.6" RenderingBias="Performance" ShadowDepth="0" />
</Setter.Value>
</Setter>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Button.Template>
</Button>
6、三态按钮及背景色、文字颜色变化
<Style x:Key="Style.OkOperationButton" TargetType="ButtonBase">
<Setter Property="Width" Value="110" />
<Setter Property="Height" Value="44" />
<Setter Property="FontSize" Value="24" />
<Setter Property="Background" Value="#FF0087FF" />
<Setter Property="HorizontalContentAlignment" Value="Center" />
<Setter Property="VerticalContentAlignment" Value="Center" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ButtonBase}">
<Border x:Name="Border" Width="{TemplateBinding Width}"
Height="{TemplateBinding Height}"
CornerRadius="22" Background="{TemplateBinding Background}">
<TextBlock x:Name="TextBlock"
Text="{TemplateBinding Content}"
FontSize="{TemplateBinding FontSize}"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background"
Value="#FF888888" />
<Setter TargetName="TextBlock" Property="Foreground" Value="#FF111111" />
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter Property="Background"
Value="#FFfcac1c" />
<Setter TargetName="TextBlock" Property="Foreground" Value="#FFFFFFFF" />
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter TargetName="Border" Property="Background" Value="#4D0087FF" />
<Setter TargetName="TextBlock" Property="Foreground" Value="#4DFFFFFF" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
提示:TemplateBinding用来实现引用时给控件复制
如:
Text="{TemplateBinding Content}",使用:Content="确定"
FontSize="{TemplateBinding FontSize}" 使用:FontSize="18"
7、自定义控件
(1)解决方案→添加新建项→自定义控件(WPF)
(2)CustomControl.cs
using System;
……
using Button = System.Windows.Controls.Button;
namespace StudentImages
{
public class CustomControl : Button
{
static CustomControl()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomControl), new FrameworkPropertyMetadata(typeof(CustomControl)));
}
public ImageSource Icon
{
get
{
return (ImageSource)GetValue(IconProperty);
}
set { SetValue(IconProperty, value); }
}
public static readonly DependencyProperty IconProperty
= DependencyProperty.Register("Icon", typeof(ImageSource), typeof(CustomControl), null);
…………
}
}
说明:
namespace StudentImages
命名空间,自动生成
public class CustomControl : Button
CustomControl控件名称,自动生成
Button是继承的类,这里手工修改,否则这个自定义控件将会没有按钮的一些属性、事件
Icon是属性的名称
ImageSource是属性的类型
(3)Themes\Generic.xaml
<Style TargetType="{x:Type local:CustomControl}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:CustomControl}">
<Border Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<Grid>
<Border x:Name="PART_Border" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" />
<Grid HorizontalAlignment="Center" VerticalAlignment="Center">
<StackPanel>
<Grid HorizontalAlignment="Center" VerticalAlignment="Center">
<Image x:Name="PART_Icon" Source="{TemplateBinding Icon}" Width="50" Height="50"/>
<Image x:Name="PART_MouseOverIcon" Visibility="Collapsed" Source="{TemplateBinding IconMouseOver}" Width="50" Height="50"/>
<Image x:Name="PART_PressIcon" Visibility="Collapsed" Source="{TemplateBinding IconPress}" Width="50" Height="50"/>
</Grid>
</StackPanel>
</Grid>
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" TargetName="PART_Border" Value="{Binding MouseOverBackground,RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:CustomControl}}}"/>
<Setter Property="BorderBrush" TargetName="PART_Border" Value="{Binding MouseOverBorderBrush,RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:CustomControl}}}"/>
<Setter Property="Visibility" TargetName="PART_MouseOverIcon" Value="Visible"/>
<Setter Property="Visibility" TargetName="PART_Icon" Value="Collapsed"/>
<Setter Property="Visibility" TargetName="PART_PressIcon" Value="Collapsed"/>
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter Property="Background" TargetName="PART_Border" Value="{Binding MouseDownBackground,RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:CustomControl}}}"/>
<Setter Property="BorderBrush" TargetName="PART_Border" Value="{Binding MouseDownBorderBrush,RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:CustomControl}}}"/>
<Setter Property="Visibility" TargetName="PART_PressIcon" Value="Visible"/>
<Setter Property="Visibility" TargetName="PART_Icon" Value="Collapsed"/>
<Setter Property="Visibility" TargetName="PART_MouseOverIcon" Value="Collapsed"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
{TemplateBinding xxx}
xxx:如果是原属性,这里直接使用;如果是自定义的,需要在cs文件中注册
源码私信
相关推荐
- JavaScript中常用数据类型,你知道几个?
-
本文首发自「慕课网」,想了解更多IT干货内容,程序员圈内热闻,欢迎关注!作者|慕课网精英讲师Lison这篇文章我们了解一下JavaScript中现有的八个数据类型,当然这并不是JavaScr...
- 踩坑:前端的z-index 之bug一二(zh1es前端)
-
IE6下浮动元素bug给IE6下的一个div设置元素样式,无论z-index设置多高都不起作用。这种情况发生的条件有三个:1.父标签position属性为relative;2.问题标签无posi...
- 两栏布局、左边定宽200px、右边自适应如何实现?
-
一、两栏布局(左定宽,右自动)1.float+margin即固定宽度元素设置float属性为left,自适应元素设置margin属性,margin-left应>=定宽元素宽度。举例:HTM...
- 前端代码需要这样优化才是一个标准的网站
-
网站由前端和后端组成,前端呈现给用户。本文将告诉您前端页面代码的优化,当然仍然是基于seo优化的。 就前端而言,如果做伪静态处理,基本上是普通的html代码,正常情况下,这些页面内容是通过页面模...
- 网页设计如何自学(初学网页设计)
-
1在Dreamweaver中搭建不同的页面,需要掌握HTML的语句了,通过调整各项数值就可以制作出排版漂亮的页面,跟着就可以学习一些可视化设计软件。下面介绍网页设计如何自学,希望可以帮助到各位。Dre...
- 1、数值类型(数值类型有)
-
1.1数据类型概览MySQL的数据类型可划分为三大类别:数值类型:旨在存储数字(涵盖整型、浮点型、DECIMAL等)。字符串类型:主要用于存储文本(诸如CHAR、VARCHAR之类)。日期/...
- 网页设计的布局属性(网页设计的布局属性是什么)
-
布局属性是网站设计中必不可少的一个重要的环节,主要用来设置网页的元素的布局,主要有以下属性。1、float:该属性设置元素的浮动方式,可以取none,left和right等3个值,分别表示不浮动,浮在...
- Grid网格布局一种更灵活、更强大的二维布局模型!
-
当涉及到网页布局时,display:flex;和display:grid;是两个常用的CSS属性,它们都允许创建不同类型的布局,但有着不同的用法和适用场景。使用flex布局的痛点当我们使...
- React 项目实践——创建一个聊天机器人
-
作者:FredrikStrandOseberg转发链接:https://www.freecodecamp.org/news/how-to-build-a-chatbot-with-react/前言...
- 有趣的 CSS 数学函数(css公式)
-
前言之前一直在玩three.js,接触了很多数学函数,用它们创造过很多特效。于是我思考:能否在CSS中也用上这些数学函数,但发现CSS目前还没有,据说以后的新规范会纳入,估计也要等很久。然...
- web开发之-前端css(5)(css前端设计)
-
显示控制一个元素的显示方式,我们可以使用display:block;display:inline-block;display:none;其中布局相关的还有两个很重要的属性:display:flex;和...
- 2024最新升级–前端内功修炼 5大主流布局系统进阶(分享)
-
获课:keyouit.xyz/14642/1.前端布局的重要性及发展历程前端布局是网页设计和开发的核心技能之一,它决定了页面元素如何组织和呈现。从早期的静态布局到现代的响应式布局,前端布局技术经历了...
- 教你轻松制作自动换行的CSS布局,轻松应对不同设备!
-
在网页设计中,自动换行的CSS布局是非常常见的需求,特别是在响应式设计中。它可以让网页内容自动适应不同屏幕尺寸,保证用户在不同设备上都能够获得良好的浏览体验。本文将介绍几种制作自动换行的CSS布局的方...
- 晨光微语!一道 CSS 面试题,伴你静享知识治愈时光
-
当第一缕阳光温柔地爬上窗台,窗外的鸟鸣声清脆悦耳,空气中弥漫着清新的气息。在这宁静美好的清晨与上午时光,泡一杯热气腾腾的咖啡,找一个舒适的角落坐下。前端的小伙伴们,先把工作的疲惫和面试的焦虑放在一边,...
- 2023 年的响应式设计指南(什么是响应式设计优缺点)
-
大家好,我是Echa。如今,当大家考虑构建流畅的布局时,没有再写固定宽度和高度数值了。相反,小编今天构建的布局需要适用于几乎任何尺寸的设备。是不是不可思议,小编仍然看到网站遵循自适应设计模式,其中它有...
- 一周热门
- 最近发表
- 标签列表
-
- HTML 教程 (33)
- HTML 简介 (35)
- HTML 实例/测验 (32)
- HTML 测验 (32)
- JavaScript 和 HTML DOM 参考手册 (32)
- HTML 拓展阅读 (30)
- HTML常用标签 (29)
- HTML文本框样式 (31)
- HTML滚动条样式 (34)
- HTML5 浏览器支持 (33)
- HTML5 新元素 (33)
- HTML5 WebSocket (30)
- HTML5 代码规范 (32)
- HTML5 标签 (717)
- HTML5 标签 (已废弃) (75)
- HTML5电子书 (32)
- HTML5开发工具 (34)
- HTML5小游戏源码 (34)
- HTML5模板下载 (30)
- HTTP 状态消息 (33)
- HTTP 方法:GET 对比 POST (33)
- 键盘快捷键 (35)
- 标签 (226)
- HTML button formtarget 属性 (30)
- CSS 水平对齐 (Horizontal Align) (30)