为控件添加事件可以分为如下所示的三步:
q 定义事件。
q 触发事件。
为了给MyLabel控件添加单击事件,需要先声明一个Click事件,代码如下:
public event EventHandler Click;
当用户在文本上单击鼠标左键后,便触发事件,只需要在MouseLeftButton被触发时触发事件即可,代码如下所示:
//当TextBox的鼠标左键按下时,触发事件。
protected void MyLabel_MouseLeftButtonDown(object sender, MouseEventArgs e)
{
RaiseClick();
}
//触过事件的函数
private void RaiseClick()
{
if (Click != null)
{
Click(this, null);
}
}
代码中为触发事件定义了RaiseClick函数,在MouseLeftButtonDown事件中处理了此函数。最后,为MyLabel添加超级链接特效,完整的控件代码如下所示:
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO;
using System.Reflection;
namespace mySampleControls
{
public class MyLabel : Control
{
#region 私有数据区
TextBlock tb;
FrameworkElement implementationRoot;
SolidColorBrush sb;
#endregion
#region 构造函数
public MyLabel()
{
//获取当前程序集实例
Assembly asm = this.GetType().Assembly;
//获取程序集的资源清单,注意xaml的命名。
Stream s = asm.GetManifestResourceStream("mySampleControls.MyLabel.xaml");
//调用InitializeFromXaml初始化控件树。
implementationRoot = this.InitializeFromXaml(new StreamReader(s).ReadToEnd());
//获取在Xaml中定义的TextBlock控件
tb = implementationRoot.FindName("tb") as TextBlock;
//定义加载事件
this.Loaded += new EventHandler(MyLabel_Loaded);
//指定宽和高
base.Width = implementationRoot.Width;
base.Height = implementationRoot.Height;
tb.MouseLeftButtonDown += new MouseEventHandler(MyLabel_MouseLeftButtonDown);
tb.MouseEnter += new MouseEventHandler(tb_MouseEnter);
tb.MouseLeave += new EventHandler(tb_MouseLeave);
}
void tb_MouseLeave(object sender, EventArgs e)
{
tb.TextDecorations = TextDecorations.None;
}
void tb_MouseEnter(object sender, MouseEventArgs e)
{
tb.TextDecorations = TextDecorations.Underline;
}
#endregion
void MyLabel_Loaded(object sender, EventArgs e)
{
UpdateLayout();
}
#region 公共属性
//定义控件的宽度
public virtual new double Width
{
get
{
return implementationRoot.Width;
}
set
{
implementationRoot.Width = value;
UpdateLayout();
}
}
//定义控件的高度
public virtual new double Height
{
get
{
return implementationRoot.Height;
}
set
{
implementationRoot.Height = value;
UpdateLayout();
}
}
//定义控件的文本设置属性
public String Text
{
get
{
return tb.Text;
}
set
{
tb.Text = value;
UpdateLayout();
}
}
//定义控件的颜色
public SolidColorBrush LabelColor
{
get
{
return (SolidColorBrush)tb.Foreground;
}
set
{
tb.Foreground = (SolidColorBrush)value;
}
}
//定义控件的背影颜色
public SolidColorBrush BackColor
{
get
{
return (SolidColorBrush)((Canvas)implementationRoot).Background;
}
set
{
((Canvas)implementationRoot).Background= (SolidColorBrush)value;
UpdateLayout();
}
}
#endregion
#region 公共事件
public event EventHandler Click;
#endregion
//当TextBox的鼠标左键按下时,触发事件。
protected void MyLabel_MouseLeftButtonDown(object sender, MouseEventArgs e)
{
RaiseClick();
}
//触过事件的函数
private void RaiseClick()
{
if (Click != null)
{
Click(this, null);
}
}
//更新布局
protected void UpdateLayout()
{
tb.SetValue(Canvas.LeftProperty, (Width - tb.ActualWidth) / 2);
tb.SetValue(Canvas.TopProperty, (Height - tb.ActualHeight) / 2);
}
}
}