原文: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(, ));

WPF获取某控件的位置,也就是偏移量的更多相关文章

  1. WPF获取原始控件样式。

    要获取WPF控件的原始样式,需要我们安装Blend for Visual Studio. 然后,我们打开Blend for Visual Studio,创建一个WPF项目. 然后,我们向页面拖动一个B ...

  2. WPF获取原始控件样式

    要获取WPF控件的原始样式,需要我们安装Blend for Visual Studio. 然后,我们打开Blend for Visual Studio,创建一个WPF项目. 然后,我们向页面拖动一个B ...

  3. WPF 获取DataGrid 控件选中的单元格信息

    获取 DataGrid 选中的单元格的信息DataGridCellInfo cell_Info = this.studentTable.SelectedCells[0]; studentTableIt ...

  4. wpf 获取image控件的图片并保存

    XMAL代码如下: <Image Name="ImageToSave" Source="Images/pic_bg.png" Grid.RowSpan=& ...

  5. WPF中获取TreeView以及ListView获取其本身滚动条的方法,可实现自行调节scoll滚动的位置(可相应获取任何控件中的内部滚动条)

    原文:WPF中获取TreeView以及ListView获取其本身滚动条的方法,可实现自行调节scoll滚动的位置(可相应获取任何控件中的内部滚动条) 对于TreeView而言: TreeViewAut ...

  6. WPF控件相对位置解析

    WPF控件相对位置的获取方法是比较简单的.对于初学者来说,掌握这一技巧的应用,可以帮助以后对WPF的深入学习,而且在实际使用中,这是一个非常常用的方法. 我们知道WPF有着比较灵活的布局方式,关于某个 ...

  7. WPF:获取DataGrid控件单元格DataGridCell

    转载:http://blog.csdn.net/jhqin/article/details/7645357 /* ------------------------------------------- ...

  8. javascript控制滚动条的位置,获取控件的位置

    一.如下是定位鼠标在视窗中的位置,先定位视窗和页面直接的距离. function getMousePoint() { var point = {x:0,y:0}; // 如果浏览器支持 pageYOf ...

  9. silverlight——获取控件相对位置

    事出有因:页面中存在滚动条,然后点击页面按钮时会进行正确性检查,如果出错在控件的位置会出现提示信息,但由于滚动条的出现,有可能在非可视区域内的控件出了问题,但目前是看不见的,因此,考虑获得出问题控件的 ...

随机推荐

  1. [Compose] 12. Two rules about Funtors

    We learn the formal definition of a functor and look at the laws they obey. Any Functor should follo ...

  2. Linux命令具体解释(2) – mv

    文件位置:      mv: /bin/mv /usr/share/man/man1/mv.1.gz 名称:      mv - move (rename) files 使用方法:      mv [ ...

  3. 【hdu 2594】Simpsons’ Hidden Talents

    Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s) ...

  4. 【codeforces 755D】PolandBall and Polygon

    time limit per test4 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  5. Linux环境变量具体解释

    设置环境变量 profile和bashrc文件 /etc/profile:在登录时,操作系统定制用户环境时使用的第一个文件.此文件为系统的每一个用户设置环境信息,当用户第一次登录时,该文件被运行. 并 ...

  6. JS类型转换规则详解

    JS类型转换规则详解 一.总结 一句话总结:JS强制类型转换中的类型名强制类型转换和其它语言不同,是类型类的构造方法,Number(mix) 一句话总结(JS类型本质):因为js是弱类型语言,所以它相 ...

  7. js进阶 10-3 jquery中为什么用document.ready方法

    js进阶 10-3  jquery中为什么用document.ready方法 一.总结 一句话总结: 1.document.ready和window.onload的区别:用哪个好? document. ...

  8. iOS BCD编码

    inline static NSData* encodeBCD(NSString *value){ //NSString *value = @"123456"; NSMutable ...

  9. ScrollView嵌套ListView问题

    ScrollView嵌套ListView问题 导致Listview  第一主角 它是 Listview 的item 显示器的数量是不完全. 这是因为item 中间 Textview 话是太多的问题 一 ...

  10. spring security之httpSecurity 专题

    37.5.2 Resolving the CsrfToken Spring Security provides CsrfTokenArgumentResolver which can automati ...