WPF图片按钮的实现方法
2020-02-06 16:00:25来源:爱站网 阅读 ()
我们在开发项目中不使用按钮,但是在WPF中有很多方法可以定义按钮的样式,也可以重写其模板,接下来,爱站技术频道小编为大家介绍的是WPF图片按钮的实现方法,一起来了解吧!
直接代码
public class ImageButton : System.Windows.Controls.Button
{
/// <summary>
/// 图片
/// </summary>
public static readonly DependencyProperty ImageProperty = DependencyProperty.Register("Image", typeof(ImageSource), typeof(ImageButton),
new PropertyMetadata(null));
/// <summary>
/// 图片的宽度
/// </summary>
public static readonly DependencyProperty ImageWidthProperty = DependencyProperty.Register("ImageWidth", typeof(double), typeof(ImageButton),
new PropertyMetadata(double.NaN));
/// <summary>
/// 图片的高度
/// </summary>
public static readonly DependencyProperty ImageHeightProperty = DependencyProperty.Register("ImageHeight", typeof(double), typeof(ImageButton),
new PropertyMetadata(double.NaN));
/// <summary>
/// 构造函数
/// </summary>
static ImageButton()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(ImageButton),
new System.Windows.FrameworkPropertyMetadata(typeof(ImageButton)));
}
/// <summary>
/// 设置图片
/// </summary>
public ImageSource Image
{
get
{
return GetValue(ImageProperty) as ImageSource;
}
set
{
SetValue(ImageProperty, value);
}
}
/// <summary>
/// 图片宽度(属性)
/// </summary>
public double ImageWidth
{
get
{
return (double)GetValue(ImageWidthProperty);
}
set
{
SetValue(ImageWidthProperty, value);
}
}
/// <summary>
/// 图片高度(属性)
/// </summary>
public double ImageHeight
{
get
{
return (double)GetValue(ImageHeightProperty);
}
set
{
SetValue(ImageHeightProperty, value);
}
}
}
样式代码
<Style TargetType="{x:Type xi:ImageButton}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type xi:ImageButton}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Border x:Name="border" Grid.RowSpan="2" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}"
SnapsToDevicePixels="true" CornerRadius="3,3,3,3"/>
<Image Grid.Row="0" Source="{TemplateBinding Image}"
Width="{TemplateBinding ImageWidth}"
Height="{TemplateBinding ImageHeight}"
VerticalAlignment="{TemplateBinding VerticalAlignment}"/>
<ContentPresenter Grid.Row="1" HorizontalAlignment="Center" Margin="{TemplateBinding Padding}"
VerticalAlignment="Center" RecognizesAccessKey="True" />
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsPressed" Value="True">
<Setter Property="Foreground" Value="#999999"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
调用实例
?
好了,以上就是爱站技术频道小编为大家精心准备的WPF图片按钮的实现方法,希望大家能够喜欢,也希望大家一如既往的支持js.aizhan.com吧!
?
原文链接:https://js.aizhan.com/develop/aspnet/11375.html
如有疑问请与原作者联系
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- WPF实现带全选复选框的列表控件 2020-03-29
- WPF实现简单的跑马灯效果 2020-03-23
- WPF实现ScrollViewer滚动到指定控件处 2020-03-19
- WPF实现定时刷新UI界面功能 2020-03-14
- ASP.NET MVC图片上传前预览简单实现 2020-01-18
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash
