简体   繁体   English

如何在Canvas中获取UserControl的X,Y位置?

[英]How do I get the X,Y position of a UserControl within a Canvas?

I have a simple UserControl implemented as below - which I place in a Canvas. 我有一个简单的UserControl实现如下 - 我放在Canvas中。 I move it using Multi-touch and I want to be able to read its new X,Y postion using procedural C# code. 我使用Multi-touch移动它,我希望能够使用程序C#代码读取它的新X,Y位置。 Ideally I would like to have X and Y as two properties or as a Point (X,Y). 理想情况下,我希望将X和Y作为两个属性或作为点(X,Y)。

<UserControl x:Class="TouchControlLibrary.myControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="64" d:DesignWidth="104">
    <Border Name="ControlBorder" BorderThickness="1" BorderBrush="Black">
        <DockPanel Margin="1" Height="60 " Width="100">
            <StackPanel  DockPanel.Dock="Left" Background="Gray"  Width="20" >
                <Button Background="#FFDEDE53" Padding="0">In</Button>
            </StackPanel>
            <StackPanel  DockPanel.Dock="Right" Background="Gray"  Width="20" >
                <Button Background="#FFE8B48F" Padding="0">Out</Button>
            </StackPanel>
        </DockPanel>  
    </Border>
</UserControl>

I expected to be able to create an attached property for each of 'X' and 'Y' and fill them from Canvas.Left and Canvas.Top, using binding or some form of attached property or maybe something else entirely. 我希望能够为'X'和'Y'中的每一个创建一个附加属性,并使用绑定或某种形式的附加属性或者完全不同的东西从Canvas.Left和Canvas.Top填充它们。

However, despite spending quite some time searching for a solution, everything I found so far seems to be 'not quite what is needed'. 然而,尽管花了相当长的时间寻找解决方案,但到目前为止我发现的所有内容似乎都“不太需要”。

What would you suggest I do to solve this problem? 你有什么建议我来解决这个问题?

let me see if I have this right, you create a new instance of the user control and add it to a canvas? 让我看看如果我有这个权利,你创建一个新的用户控件实例并将其添加到画布? Then you move it using Multi-touch (how do you do this? via a behaviour or manually?), and at any one time you want to read the new X/Y position of the instance of the user control relative to the Canvas? 然后使用Multi-touch移动它(你怎么做?通过行为或手动?),并且在任何时候你想要读取用户控件实例相对于Canvas的新X / Y位置?

Why cant you just read the same thing that is being changed when you move it via touch? 为什么你不能读到通过触摸移动它时被改变的东西? Have you had a look at the RenderTransform property, this is what is usually changed when you manipulate something using a behavior or manual adjustments. 您是否看过RenderTransform属性,当您使用行为或手动调整操作某些内容时,通常会更改此属性。

Well, here's a sample code i've for to get the Y, you can figure out the X hope this is what you need: 好吧,这是我要获得Y的示例代码,你可以找出X希望这是你需要的:

Point current = e.GetPosition(MyControl as UIElement); //where your control is
Point top = e.GetPosition(Canvas as UIElement); //or maybe just the height of the canvas
Thickness margin = new Thickness();
margin.Top = top.Y - current.Y; //distance from the top
MyControl.Margin = margin;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM