【问题标题】:How to get the Left,Top,Right,Bottom point of a UIElement in WPF?如何在 WPF 中获取 UIElement 的左、上、右、下点?
【发布时间】:2012-09-26 13:32:54
【问题描述】:

我想找到UIElementLeft,Top,Right,Bottom。我试过但没有结果。

这里有人知道如何获得这些吗?

实际上,我正在 WPF 中创建一个自定义面板。

我不想从 Canvas 继承。

Size elemSize = this.ElementBeingDragged.DesiredSize;
// Get the element's offsets from the four sides of the Panel.
Vector v= VisualTreeHelper.GetOffset(this.ElementBeingDragged);
double left = v.X;
double right = v.X + elemSize.Width;
double top = v.Y;
double bottom = v.Y+elemSize.Height;

【问题讨论】:

  • 你试过 TranslatePoint 或 TransformToAncestor 吗?

标签: c# wpf panel uielement


【解决方案1】:

试试

Point position = ElementBeingDragged.TranslatePoint(new Point(0, 0), UIElementRelativeTo);

例如获取相对于包含窗口的点:

Point position = ElementBeingDragged.TranslatePoint(new Point(0, 0), Window.GetWindow(ElementBeingDragged));

【讨论】:

    【解决方案2】:

    您可以尝试使用此代码 - 基于 TransformToAncestor

    private Point GetPosition(Visual item) 
    {
       var transformToAncestor = item.TransformToAncestor(MyForm);
    
       var position = transformToAncestor.Transform(new Point(0, 0));
    
       return position;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多