Code
<Grid x:Name="LayoutRoot" Background="White">
<Grid HorizontalAlignment="Left" VerticalAlignment="Top" Name="grid1" Background="Red" Width="100" Height="100" MouseEnter="Grid_MouseEnter"
MouseMove="Grid_MouseMove"
MouseLeave="Grid_MouseLeave"
>
</Grid>
<Grid HorizontalAlignment="Left" VerticalAlignment="Top" Name="grid2" Margin="150,0,0,0" Background="Green" Width="100" Height="100" MouseMove="Grid_MouseMove_1">
</Grid>
<Grid HorizontalAlignment="Left" VerticalAlignment="Top" Name="grid3" Background="Blue" Margin="300,0,0,0" Width="100" Height="100" MouseMove="Grid_MouseMove_2">
</Grid>
<TextBlock Name="label1" Margin="20,200,0,0">
<Run Foreground="Red" >
mouse 离grid1左上角距离:
</Run>
<LineBreak></LineBreak>
<Run Foreground="Green">
mouse 离grid2左上角距离:
</Run>
<LineBreak/>
<Run Foreground="Blue">
mouse 离grid3左上角距离:
</Run>
</TextBlock>
</Grid>
<Grid x:Name="LayoutRoot" Background="White">
<Grid HorizontalAlignment="Left" VerticalAlignment="Top" Name="grid1" Background="Red" Width="100" Height="100" MouseEnter="Grid_MouseEnter"
MouseMove="Grid_MouseMove"
MouseLeave="Grid_MouseLeave"
>
</Grid>
<Grid HorizontalAlignment="Left" VerticalAlignment="Top" Name="grid2" Margin="150,0,0,0" Background="Green" Width="100" Height="100" MouseMove="Grid_MouseMove_1">
</Grid>
<Grid HorizontalAlignment="Left" VerticalAlignment="Top" Name="grid3" Background="Blue" Margin="300,0,0,0" Width="100" Height="100" MouseMove="Grid_MouseMove_2">
</Grid>
<TextBlock Name="label1" Margin="20,200,0,0">
<Run Foreground="Red" >
mouse 离grid1左上角距离:
</Run>
<LineBreak></LineBreak>
<Run Foreground="Green">
mouse 离grid2左上角距离:
</Run>
<LineBreak/>
<Run Foreground="Blue">
mouse 离grid3左上角距离:
</Run>
</TextBlock>
</Grid>
Code
public partial class Page : UserControl
{
public Page()
{
InitializeComponent();
}
private void Grid_MouseEnter(object sender, MouseEventArgs e)
{
}
private void Grid_MouseMove(object sender, MouseEventArgs e)
{
this.label1.Text="Mouse 离grid1距离为:"+e.GetPosition(this.grid1)+"\r\n";
this.label1.Text += "Mouse 离grid2距离为:" + e.GetPosition(this.grid2) + "\r\n";
this.label1.Text += "Mouse 离grid3距离为:" + e.GetPosition(this.grid3);
}
private void Grid_MouseLeave(object sender, MouseEventArgs e)
{
}
private void Grid_MouseMove_1(object sender, MouseEventArgs e)
{
//this.label1.d = "<Run Foreground=\"Red\" > mouse 离grid1中心距离:</Run>" + e.GetPosition(this.grid1) + "<LineBreak></LineBreak>";
this.label1.Text = "Mouse 离grid1距离为:" + e.GetPosition(this.grid1) + "\r\n";
this.label1.Text += "Mouse 离grid2距离为:" + e.GetPosition(this.grid2) + "\r\n";
this.label1.Text += "Mouse 离grid3距离为:" + e.GetPosition(this.grid3);
}
private void Grid_MouseMove_2(object sender, MouseEventArgs e)
{
this.label1.Text = "Mouse 离grid1距离为:" + e.GetPosition(this.grid1) + "\r\n";
this.label1.Text += "Mouse 离grid2距离为:" + e.GetPosition(this.grid2) + "\r\n";
this.label1.Text += "Mouse 离grid3距离为:" + e.GetPosition(this.grid3);
}
}
public partial class Page : UserControl
{
public Page()
{
InitializeComponent();
}
private void Grid_MouseEnter(object sender, MouseEventArgs e)
{
}
private void Grid_MouseMove(object sender, MouseEventArgs e)
{
this.label1.Text="Mouse 离grid1距离为:"+e.GetPosition(this.grid1)+"\r\n";
this.label1.Text += "Mouse 离grid2距离为:" + e.GetPosition(this.grid2) + "\r\n";
this.label1.Text += "Mouse 离grid3距离为:" + e.GetPosition(this.grid3);
}
private void Grid_MouseLeave(object sender, MouseEventArgs e)
{
}
private void Grid_MouseMove_1(object sender, MouseEventArgs e)
{
//this.label1.d = "<Run Foreground=\"Red\" > mouse 离grid1中心距离:</Run>" + e.GetPosition(this.grid1) + "<LineBreak></LineBreak>";
this.label1.Text = "Mouse 离grid1距离为:" + e.GetPosition(this.grid1) + "\r\n";
this.label1.Text += "Mouse 离grid2距离为:" + e.GetPosition(this.grid2) + "\r\n";
this.label1.Text += "Mouse 离grid3距离为:" + e.GetPosition(this.grid3);
}
private void Grid_MouseMove_2(object sender, MouseEventArgs e)
{
this.label1.Text = "Mouse 离grid1距离为:" + e.GetPosition(this.grid1) + "\r\n";
this.label1.Text += "Mouse 离grid2距离为:" + e.GetPosition(this.grid2) + "\r\n";
this.label1.Text += "Mouse 离grid3距离为:" + e.GetPosition(this.grid3);
}
}