我有一个简单的 UserControl 实现如下 - 我将其放置在 Canvas 中。我使用多点触控移动它,我希望能够使用程序 C# 代码读取其新的 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>
我希望能够为“X”和“Y”中的每一个创建一个附加属性,并使用绑定(bind)或某种形式的附加属性或完全使用其他东西从 Canvas.Left 和 Canvas.Top 填充它们。
然而,尽管我花了相当多的时间寻找解决方案,但到目前为止我发现的一切似乎都“不是所需要的”。
您建议我如何解决这个问题?
最佳答案
让我看看我是否有这个权利,你创建一个新的用户控件实例并将它添加到 Canvas ?然后你使用多点触控移动它(你如何做到这一点?通过行为还是手动?),并且在任何时候你想要读取用户控件实例相对于 Canvas 的新 X/Y 位置?
当您通过触摸移动它时,为什么您不能只读取正在更改的同一内容?您是否看过 RenderTransform 属性,当您使用行为或手动调整操作某些内容时,通常会更改此属性。
关于c# - 如何获取 Canvas 中 UserControl 的 X、Y 位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5039887/