WPF怎么获取一个控件的坐标啊

2024-12-02 07:02:35
推荐回答(2个)
回答1:

  参考方法有以下二种:
  方法一:
  //_stackPanel为子元素,_grid为父元素
  Point point = _stackPanel.TranslatePoint(new Point(0, 0), (UIElement)_grid);
  
  方法二:
  //_stackPanel为子元素,_grid为父元素
  GeneralTransform generalTransform = _stackPanel.TransformToAncestor(_grid);
  Point point = generalTransform.Transform(new Point(0, 0));
  
  用到的NameSpace
  using System.Windows;
  using System.Windows.Media;

回答2:

Window window = Window.GetWindow(控件);Point point = 控件.TransformToAncestor(window).Transform(new Point(0, 0));