8,757
社区成员
发帖
与我相关
我的任务
分享using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
namespace JiaBoo.Controls.Buttons
{
/// <summary>
/// 包含图片的按钮类
/// </summary>
public class ImageButton : Button
{
public static readonly DependencyProperty ImageSourceProperty;
//public static readonly DependencyProperty ContentProperty;
static ImageButton()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(ImageButton), new FrameworkPropertyMetadata(typeof(ImageButton)));
ImageSourceProperty = DependencyProperty.Register("ImageSource", typeof(ImageSource), typeof(ImageButton), new FrameworkPropertyMetadata(null));
ContentProperty.OverrideMetadata(typeof(ImageButton), new FrameworkPropertyMetadata("ImageButton"));
}
/// <summary>
/// 图片
/// </summary>
public ImageSource ImageSource
{
get { return (ImageSource)GetValue(ImageSourceProperty); }
set { SetValue(ImageSourceProperty, value); }
}
public ImageButton()
{
this.Width = 90;
this.Height = 30;
this.Content = "ImageButton";
}
}
}
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:JiaBoo.Controls.Buttons">
<Style TargetType="{x:Type local:ImageButton}">
<Setter Property="Background">
<Setter.Value>
<LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
<LinearGradientBrush.GradientStops>
<GradientStop Color="#80CDCDCD" Offset="0" />
<GradientStop Color="#FFCDCDCD" Offset="0.5" />
</LinearGradientBrush.GradientStops>
</LinearGradientBrush>
</Setter.Value>
</Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:ImageButton}">
<Grid Name="TemplateRoot" SnapsToDevicePixels="True">
<Rectangle RadiusX="2" RadiusY="2" Fill="{TemplateBinding Background}" />
<Border CornerRadius="2,2,2,2" Margin="1,1,1,1">
<Border.Background>
<LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
<LinearGradientBrush.GradientStops>
<GradientStop Color="#50FFFFFF" Offset="0.5385" />
<GradientStop Color="#00FFFFFF" Offset="0.5385" />
</LinearGradientBrush.GradientStops>
</LinearGradientBrush>
</Border.Background>
</Border>
<Border BorderThickness="1,0,1,1" BorderBrush="#80FFFFFF" Margin="1,1,1,1">
<Border.Background>
<LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
<LinearGradientBrush.GradientStops>
<GradientStop Color="#80FFFFFF" Offset="0.05" />
<GradientStop Color="#00FFFFFF" Offset="0.25" />
</LinearGradientBrush.GradientStops>
</LinearGradientBrush>
</Border.Background>
</Border>
<Grid Margin="2,2,2,2">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*"></ColumnDefinition>
<ColumnDefinition Width="2*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Rectangle Margin="5">
<Rectangle.Fill>
<ImageBrush AlignmentX="Center" AlignmentY="Center" ImageSource="{TemplateBinding ImageSource}"></ImageBrush>
</Rectangle.Fill>
</Rectangle>
<Image Margin="5" Source="{TemplateBinding ImageSource}"/>
<ContentPresenter Grid.Column="1" RecognizesAccessKey="True" Margin="5,0,0,0" Content="{TemplateBinding Content}" ContentStringFormat="{TemplateBinding ContentControl.ContentStringFormat}" HorizontalAlignment="Left" VerticalAlignment="Center" SnapsToDevicePixels="True" />
</Grid>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>