WPF获取某控件的位置,也就是偏移量
生活随笔
收集整理的這篇文章主要介紹了
WPF获取某控件的位置,也就是偏移量
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
WPF獲取某控件的位置,也就是偏移量 原文:WPF獲取某控件的位置,也就是偏移量
此段示例在MSDN中可見。XAML代碼如下:
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" ><StackPanel Margin="16"><StackPanel Margin="8"><TextBlock Name="myTextBlock" Margin="4" Text="Hello, world" /></StackPanel></StackPanel> </Window>?
1、如果只需要獲取相對于其父級的偏移量,則可以使用以下方法:
// Return the offset vector for the TextBlock object. Vector vector = VisualTreeHelper.GetOffset(myTextBlock);// Convert the vector to a point value. Point currentPoint = new Point(vector.X, vector.Y);?
偏移量保存在Vector對象中
2、相對靈活的方法可以使用?TransformToAncestor方法,這樣可以獲得相對于Window的偏移量
// Return the general transform for the specified visual object. GeneralTransform generalTransform1 = myTextBlock.TransformToAncestor(this);// Retrieve the point value relative to the parent. Point currentPoint = generalTransform1.Transform(new Point(0, 0));?
?
posted on 2018-07-11 01:23 NET未來之路 閱讀(...) 評論(...) 編輯 收藏轉載于:https://www.cnblogs.com/lonelyxmas/p/9292373.html
總結
以上是生活随笔為你收集整理的WPF获取某控件的位置,也就是偏移量的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: PHP内置函数生成随机数的方法汇总
- 下一篇: DB知识点记录