在asp.net里按钮都有Click事件,那么在xaml界面里,怎么触发鼠标点击事件呢?其实在xaml里正对鼠标有
MouseEnter MouseLeave MouseLeftButtonDown MouseLeftButtonUp 事件
我们在鼠标的MouseLeftButtonUp 事件触发的时候就调用我们自己写的事件就可以实现鼠标左键点击事件了。
代码片段如下:
public event EventHandler Click;
// 触发点击事件
protected void RaiseClick()
{
if (Click != null)
{
Click(this, null);
}
}
void actControl_MouseLeftButtonUp(object sender, MouseEventArgs e)
{
ReleaseMouseCapture();
RaiseClick();
}
这样我们就给按钮控件加上Click事件。
我们回到上一篇给出的wowmovie项目,在page.xmal.cs里面加上如下代码:
public void Page_Loaded(object o, EventArgs e)
{
double top = 100;
double left = 20;
ButtonControl txtbox = new ButtonControl();
txtbox.ButtonText = "Hello!";
txtbox.SetValue<double>(Canvas.TopProperty, top);
txtbox.SetValue<double>(Canvas.LeftProperty, left);
txtbox.Click += new EventHandler(txtbox_Click);
this.Children.Add(txtbox);
}
void txtbox_Click(object sender, EventArgs e)
{
ButtonControl bt = sender as ButtonControl;
bt.ButtonText = "You Click Me";
}
{
double top = 100;
double left = 20;
ButtonControl txtbox = new ButtonControl();
txtbox.ButtonText = "Hello!";
txtbox.SetValue<double>(Canvas.TopProperty, top);
txtbox.SetValue<double>(Canvas.LeftProperty, left);
txtbox.Click += new EventHandler(txtbox_Click);
this.Children.Add(txtbox);
}
void txtbox_Click(object sender, EventArgs e)
{
ButtonControl bt = sender as ButtonControl;
bt.ButtonText = "You Click Me";
}
效果如下:
点击之后变成这个样子。