之前一直做WPF现在开始接触Slilverlight感触很多。
今天做一个Button要求
有两个图片,button默认有一个图片,鼠标over时用另一个图片,
用wpf做的时候写一个template很简单,但silverlight和wpf写起来不一样
记录一下。大概思路是两个image鼠标MouseOver的时候一个Visible一个Collapsed
写的是一个自定义控件,代码和皮肤分离,很简单的一个demo
代码下载:ImageButtonTest.rar
先写一个继承自button的imagebutton类
namespace ImageButtonTest
{
/// <summary>
/// build by lp
/// </summary>
public class MyImageButton : Button
{
public static readonly DependencyProperty ImageNormalProperty =
DependencyProperty.Register(\"ImageNormal\",
typeof(ImageSource),
typeof(MyImageButton),
new PropertyMetadata(null));
public static readonly DependencyProperty ImageHoverProperty =
DependencyProperty.Register(\"ImageHover\",
typeof(ImageSource),
typeof(MyImageButton),
new PropertyMetadata(null));
//鼠标移到上面
public ImageSource ImageHover
{
get { return (ImageSource)GetValue(ImageHoverProperty); }
set { SetValue(ImageHoverProperty, value); }
}
//默认图片
public ImageSource ImageNormal
{
get { return (ImageSource)GetValue(ImageNormalProperty); }
set { SetValue(ImageNormalProperty, value); }
}
public MyImageButton()
{
this.DefaultStyleKey = typeof(MyImageButton);
}
}
}
本文地址:https://www.stayed.cn/item/6097
转载请注明出处。
本站部分内容来源于网络,如侵犯到您的权益,请 联系我