C#_WPF_按钮模板及自定义控件的使用
liuian 2025-05-02 19:36 98 浏览
源码私信联系
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文件中注册
源码私信
相关推荐
- 笔记本电脑硬盘多少钱一个(4t机械硬盘多少钱)
-
这要看多大的容量的,是不是好拆。如果换120gsata接口的固态,选便宜的影驰这样的品牌的。换好装好系统。360左右的价格吧。我自己换的,我把原先的硬盘位换成固态硬盘,把光驱位通过硬盘托把笔记本的硬盘...
- 惠普电脑专卖店地址(惠普电脑型号查询官网)
-
京东的惠普的授权旗舰店和京东自营旗舰店比较靠谱不论是惠普的授权旗舰店,还是京东自营旗舰店,他们卖的产品都是靠谱的,不会存在故意坑你的情况,但是相比之下,京东自营的服务更有优势:京东自营,走的是京东物流...
-
- 电脑黑屏如何解决方法(电脑黑屏如何解决方法图片)
-
查看是否是由于显示器自身的原因而不能正常工作造成的黑屏,将显示屏的插头先拆下来,进行一次清理,然后再重新连接。如果还是不行,可以试一下其他的接口或者显示屏,看看是不是显示屏或者是接口的问题。如果显卡没有信号送出或有信号送不出去时,这时显示器...
-
2025-12-24 15:55 liuian
-
- 移动硬盘windows无法完成格式化
-
一,移动硬盘故障,无法被Windows识别和读取,也就无法被格式化了。二,某些移动硬盘盒的芯片与Windows兼容性不好,可以换其它的硬盘盒试一下。三,如果移动硬盘原先接在苹果电脑或linux系统下,其格式是与Windows不兼容的。方法/...
-
2025-12-24 15:05 liuian
- win10所有快捷键失效(win10快捷键失效最简单三个步骤)
-
如果Win10的快捷键失灵了,你可以尝试以下解决方法:1.检查键盘连接是否稳固,重新插拔键盘。2.检查键盘设置,确保快捷键功能未被禁用。3.更新键盘驱动程序,可以通过设备管理器或官方网站下载最新驱动。...
- win10快捷关机(windows10快捷关机键)
-
按下Win键+X,然后选择“关机或注销”,再点击“关机”选项即可快捷关机。因为Win键+X是打开快速菜单的快捷键,通过选择“关机或注销”,再点击“关机”选项,可以快速关闭电脑。此外,还可以通过按下Al...
- 笔记本电脑是否有gpu(笔记本电脑是否有国补)
-
需要安装一个新的独立的显卡,或者是更换一下主板,用主板上的集成显卡,gPU是电脑最主要的配置,没有的话电脑无法开机进行。意思如下:如果是CPU和显卡的占用率都比较低,那说明性能过剩,可以把游戏的特效...
-
- win7家庭组共享打印机(win7家庭普通版连接共享打印机)
-
1、首先在win7电脑桌面的主页面下方点击网络图标,并且在弹出来的页面选择。【打开网络和共享中心】2、接着点击【更改高级共享设置】。3、最后在共享设置页面,点击【启用文件和打印机共享】就可以共享此电脑上的打印机了。Win7系统连接网络打印机...
-
2025-12-24 13:05 liuian
-
- 超好看的动态手机壁纸(超好看的动态手机壁纸图片)
-
可以进入【手机i主题--右上角人形图标--壁纸】,设置动态壁纸。其它情况下手机本身不支持将动态图片和视频设置成桌面壁纸和锁屏。您可以尝试使用第三方软件实现。备注:抖音的参考设置方法:【抖音界面-登录抖音账号-找到视频-点击右下角的“分享”按...
-
2025-12-24 12:05 liuian
- chrome安卓版2025最新版本(chrome稳定版 安卓)
-
手机谷歌地球打不开,一直转圈的话,可能是因为你的网络不太稳定,你可以换一个网络googleearth大概1~2年更新一次。用户可以通过googleearth来查看更新时间。将earth放大到一...
- win10精简版msdn(win10精简版是哪个版本)
-
这个问题只能由技术人员或有使用经验的人回答,无法准确回答“哪个好”。因为msdn官网提供的win7精简版,有不同的版本、不同的功能和不同的定制化程度。选择哪个版本、哪个标准或哪种方案,需要根据具体需...
- 笔记本电脑硬盘分区教程图解
-
第一步:进入磁盘管理器。 在笔记本电脑的桌面找到“计算机”图标,然后点击右键,在下拉列表中找到“管理”选项,点击打开,进入到“计算机管理”界面。然后选择左侧列表中的“磁盘管理”选线,点击打开。 ...
- 还原精灵使用方法(还原精灵使用方法视频)
-
还原精灵是一种用于恢复被损坏或删除的文件的工具。使用方法如下:首先,打开还原精灵软件。然后,选择要恢复的文件类型或指定文件路径。接下来,点击“扫描”按钮,软件将开始扫描并列出可恢复的文件。在列表中选择...
- 一周热门
- 最近发表
- 标签列表
-
- python判断字典是否为空 (50)
- crontab每周一执行 (48)
- aes和des区别 (43)
- bash脚本和shell脚本的区别 (35)
- canvas库 (33)
- dataframe筛选满足条件的行 (35)
- gitlab日志 (33)
- lua xpcall (36)
- blob转json (33)
- python判断是否在列表中 (34)
- python html转pdf (36)
- 安装指定版本npm (37)
- idea搜索jar包内容 (33)
- css鼠标悬停出现隐藏的文字 (34)
- linux nacos启动命令 (33)
- gitlab 日志 (36)
- adb pull (37)
- python判断元素在不在列表里 (34)
- python 字典删除元素 (34)
- vscode切换git分支 (35)
- python bytes转16进制 (35)
- grep前后几行 (34)
- hashmap转list (35)
- c++ 字符串查找 (35)
- mysql刷新权限 (34)
