例子是微软的, 在代码里加了一个MouseRelease的事件, 模仿鼠标的click事件,
代码比较乱, 随便看看

复制内容到剪贴板
代码:
<Canvas xmlns="http://schemas.microsoft.com/client/2007"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="130" Height="30"
Background="Silver"
>
<TextBlock x:Name="tb"></TextBlock>
</Canvas>复制内容到剪贴板
代码:
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;
namespace CustomerControl
{
public class MyLabel : Control
{
public delegate void mouseRelease(object sender, MouseEventArgs e);
public event mouseRelease MouseRelease;
bool mouseInthis=false, mouseDown=false;
FrameworkElement implementationRoot;
TextBlock tb;
public MyLabel()
{
System.IO.Stream s = this.GetType().Assembly.GetManifestResourceStream("CustomerControl.MyLabel.xaml");
implementationRoot = this.InitializeFromXaml(new System.IO.StreamReader(s).ReadToEnd());
tb = implementationRoot.FindName("tb") as TextBlock;
this.Loaded += new EventHandler(MyLabel_Loaded);
implementationRoot.MouseEnter += new MouseEventHandler(onMouseEnter);
implementationRoot.MouseLeave += new EventHandler(onMouseLeave);
implementationRoot.MouseLeftButtonDown += new MouseEventHandler(onMouseDown);
implementationRoot.MouseLeftButtonUp += new MouseEventHandler(onMouseUp);
base.Width = implementationRoot.Width;
base.Height = implementationRoot.Height;
}
void MyLabel_Loaded(object sender, EventArgs e)
{
UpdateLayout();
}
public virtual new double Height
{
get
{
return implementationRoot.Height;
}
set
{
implementationRoot.Height = value;
UpdateLayout();
}
}
public virtual new double Width
{
get
{
return implementationRoot.Width;
}
set
{
implementationRoot.Width = 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;
}
}
protected void UpdateLayout()
{
tb.SetValue(Canvas.LeftProperty, (Width - tb.ActualWidth) / 2);
tb.SetValue(Canvas.TopProperty, (Height - tb.ActualHeight) / 2);
//tb.SetValue(Canvas.LeftProperty, 0);
//tb.SetValue(Canvas.TopProperty, 0);
}
void onMouseEnter(object sender, MouseEventArgs e)
{
mouseInthis = true;
}
void onMouseLeave(object sender, EventArgs e)
{
mouseInthis = false;
mouseDown = false;
}
void onMouseDown(object sender, MouseEventArgs e)
{
mouseDown = true;
}
void onMouseUp(object sender, MouseEventArgs e)
{
if (mouseDown && mouseInthis)
{
mouseDown = false;
MouseRelease(sender, e);
}
}
}
}