silverlight 鼠标事件处理
程序员文章站
2022-06-24 09:12:54
托管代码注册鼠标事件不需要再.xaml中声明注册事件。.xaml 区别于在.xaml中注册事件 然后再.c# 中进行鼠标事件的注册,放在MainPage 中public MainPage() { InitializeComponent(); ... ......
托管代码注册鼠标事件
不需要再.xaml中声明注册事件。
.xaml
<Ellipse Name="ellipse" Width="120" Height="120" Fill="Orange" Canvas.Top="60" Canvas.Left="80"/>
区别于在.xaml中注册事件
<Ellipse Name="ellipse" Width="120" Height="120" Fill="Orange" Canvas.Top="60" Canvas.Left="80" MouseEnter="OnMouseEnter"/>
然后再.c# 中进行鼠标事件的注册,放在MainPage 中
public MainPage() { InitializeComponent(); ellipse.MouseEnter += new MouseEnterHander(OnMouseEnter); ellipse.MouseLeave += new MouseEnterHander(OnMouseEnter);
}
void OnMouseEnter(object sender, MouseEventArgs e)
{
Ellipse m = sender as Ellipse;
m.Fill=new SolidColorBrush(Color.Green);
}
在获取时间的时候可以在
ellipse.MouseEnter += 直接连击Tab
public MainPage() { InitializeComponent(); ellipse.MouseEnter += Ellipse_MouseEnter; ellipse.MouseLeave += Ellipse_MouseLeave; } private void Ellipse_MouseLeave(object sender,MouseEventArgs e) { Ellipse ell = sender as Ellipse; ell.Fill = new SolidColorBrush(Colors.Gray); } private void Ellipse_MouseEnter(object sender, MouseEventArgs e) { Ellipse ell = sender as Ellipse; ell.Fill = new SolidColorBrush(Colors.Black); }