使用 PointToScreen 在 wpf 中查找控件的位置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24802945/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Find location of a control in wpf using PointToScreen
提问by Max Mazur
I'm trying to find my relative coordinate for a usercontrol inside my mainwindow. I tried using the "Control.PointToScreen()" method but with no luck. Everytime I do this i get an exception that says:
我试图在我的主窗口中找到我的用户控件的相对坐标。我尝试使用“Control.PointToScreen()”方法,但没有成功。每次我这样做时,我都会得到一个例外,说:
System.InvalidOperationException: This Visual is not connected to a PresentationSource
System.InvalidOperationException: 此 Visual 未连接到 PresentationSource
I think this has something to do with me calling pointToScreen before the visuals have rendered properly, since i'm already calling the method in my Mains Constructor.
我认为这与我在视觉效果正确呈现之前调用 pointToScreen 有关,因为我已经在 Mains Constructor 中调用了该方法。
Anyways, I would like to hear if anyone of you had a hint/Solution/Idea how I could perhaps work around this.
无论如何,我想听听你们中是否有人有任何提示/解决方案/想法,我或许可以解决这个问题。
Just to clearify what i'm trying to do, my control contains a photocontrol which i need the exact location off inside my maincontrol, since i want to use these coordinates to create a duplicate of the control on top of it
只是为了明确我要做什么,我的控件包含一个光控,我需要在我的主控件中的确切位置,因为我想使用这些坐标在它上面创建控件的副本
Experimenting with PointToScreen.
尝试使用 PointToScreen。
回答by sebhaub
In your code register for the Loaded event of the UserControl. That should fix the bug that the visuals have not been rendered yet when you try to get the position.
在您的代码中注册 UserControl 的 Loaded 事件。这应该修复当您尝试获取位置时视觉效果尚未呈现的错误。
YourControl.Loaded += ControlLoaded;
public void ControlLoaded(object sender, EventArgs e){
Console.WriteLine(YourControl.PointToScreen(new Point(0,0));
}
Edit
编辑
Since you want the position of your control relative to your window, better try that one .
由于您想要控件相对于窗口的位置,最好尝试一下。
YourControl.TransformToAncestor(YourWindow).Transform(new Point(0,0))
回答by Max Mazur
Since Contend rendered didn't work I found a solution for my problem.
由于 Contend 渲染不起作用,我找到了解决问题的方法。
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
ItemsControl ItemsControl = UCEnvironmentControl.GetItemsControlPhotos();
Dispatcher.BeginInvoke(DispatcherPriority.Loaded, new Action(() => Control.PointToScreen(new Point(0,0));
}
This way, it fires in the LoadedEvent, but waits for the content to be rendered, and then finally it gives you your coordinate back and sets your control
这样,它在 LoadedEvent 中触发,但等待内容被呈现,然后最后它给你你的坐标并设置你的控制
回答by volta
The solution from Max Mazur is working for me:
Max Mazur 的解决方案对我有用:
public partial class MainWindow : Window
{
Point locationOfYourControl;
public MainWindow()
{
InitializeComponent();
this.ContentRendered += MainWindow_ContentRendered;
}
private void MainWindow_ContentRendered(object sender, EventArgs e)
{
locationOfYourControl = YourControl.PointToScreen(new Point(0, 0));
}
}

