如何在wpf中获得鼠标相对当前控件的位置

如题所述

<DockPanel MouseMove="DockPanel_MouseMove" Width="300" Height="200" VerticalAlignment="Center" HorizontalAlignment="Center" Background="White">
<Border BorderBrush="Silver" BorderThickness="3">
<Grid>
<TextBlock Name="txt" Text="test"></TextBlock>
</Grid>
</Border>
</DockPanel>

private void DockPanel_MouseMove(object sender, MouseEventArgs e)
{
Point p = e.GetPosition((IInputElement)sender);
txt.Text = "x:" + p.X + " y:" + p.Y;
}

使用DockPanel的示例如上代码所示,注册MouseMove事件,在后台进行处理就Ok了
温馨提示:答案为网友推荐,仅供参考