如何在wpf中获得鼠标相对当前控件的位置
发布网友
发布时间:2022-05-20 06:59
我来回答
共1个回答
热心网友
时间:2023-10-06 04:40
<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了