silverlight自定义控件 10
这个不难,重定义控件就ok
代码给你参考:
类1:
[TemplatePart(Name = BorderName, Type = typeof(Border))]
public class ImageItem : ButtonBase
{
private const string BorderName = "Part_Border";
public ImageItem()
{
this.DefaultStyleKey = typeof(ImageItem);
this.MouseEnter += new MouseEventHandler(ImageItem_MouseEnter);
this.MouseLeave += new MouseEventHandler(ImageItem_MouseLeave);
}
void ImageItem_MouseLeave(object sender, MouseEventArgs e)
{
var border = this.GetTemplateChild(BorderName) as Border;
border.BorderBrush = new SolidColorBrush(Colors.Transparent);
}
void ImageItem_MouseEnter(object sender, MouseEventArgs e)
{
var border = this.GetTemplateChild(BorderName) as Border;
border.BorderBrush = new SolidColorBrush(Colors.Orange);
}
# region ImageSource
public static readonly DependencyProperty ImageSourceProperty =
DependencyProperty.Register("ImageSource", typeof(ImageSource),
typeof(ImageItem), new PropertyMetadata(null));
public ImageSource ImageSource
{
get { return (ImageSource)GetValue(ImageSourceProperty); }
set { SetValue(ImageSourceProperty, value); }
}
# endregion
# region Caption
public static readonly DependencyProperty CaptionProperty =
DependencyProperty.Register("Caption", typeof(string),
typeof(ImageItem), new PropertyMetadata(string.Empty));
public string Caption
{
get { return (string)GetValue(CaptionProperty); }
set { SetValue(CaptionProperty, value); }
}
#endregion
}
类2:
[TemplatePart(Name = BorderName, Type = typeof(Border))]
public class ImageButton : ImageItem
{
private const string BorderName = "Part_Border";
private Brush borderBrush;
public ImageButton()
{
this.DefaultStyleKey = typeof(ImageButton);
this.MouseEnter += ImageButton_MouseEnter;
this.MouseLeave += ImageButton_MouseLeave;
}
void ImageButton_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
{
var border = this.GetTemplateChild(BorderName) as Border;
borderBrush = border.BorderBrush;
border.BorderBrush = new SolidColorBrush(Colors.Transparent);
border.Background = new SolidColorBrush(Colors.Transparent);
}
void ImageButton_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
{
var border = this.GetTemplateChild(BorderName) as Border;
border.BorderBrush = borderBrush;
border.Background = new SolidColorBrush(Color.FromArgb(100, 142, 217, 205));
}:
}
使用时加入头文件:xmlns:my="clr-namespace:***类所在的域名空间*****"
//样式 1
<Style TargetType="my:ImageButton">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="my:ImageButton">
<Border Background="Transparent" CornerRadius="4"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}" x:Name="Part_Border">
<StackPanel Orientation="Horizontal" Cursor="Hand" Margin="2">
<Image Source="{TemplateBinding ImageSource}" Stretch="None" />
<TextBlock x:Name="Part_Caption" Text="{TemplateBinding Caption}" Margin="5,0,0,0" FontSize="12" />
</StackPanel>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<my:ImageButton Caption="返回" ImageSource="/BANK-S;component/Images/image06.png" Width="70" Command="{Binding Return}"/>
/*
Caption 相对与“按钮一”
ImageSource 图片路径
Command 点击事件
*/
效果跟你提供的图片一样。
//样式 2
<Style TargetType="my:ImageItem">
<Setter Property="Control.Template">
<Setter.Value>
<ControlTemplate TargetType="my:ImageItem">
<Border
x:Name="Part_Border"
CornerRadius="2.5"
BorderBrush="Transparent"
BorderThickness="2">
<StackPanel Orientation="Vertical" Cursor="Hand" Margin="5">
<Image Width="48" Height="48" Source="{TemplateBinding ImageSource}" />
<TextBlock HorizontalAlignment="Center" Text="{TemplateBinding Caption}" FontSize="12" />
</StackPanel>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<my:ImageItem Caption="按钮一" Width="84" Height="80" Margin="2,10,1,10" ImageSource="/BANK-S;component/Images/pic.png" Command="{Binding Data}" >
/*
Caption 文字“按钮一”
ImageSource 图片路径
Command 点击事件
*/
效果截图。