【发布时间】:2021-04-12 22:17:47
【问题描述】:
我在翻译点时遇到了一些问题。我想从控件中心到鼠标画一条线。我目前正在使用 GiveFeedback 事件,主要是因为我能够通过它获得一些结果——我还有其他处理程序也在使用。请注意,我有一个覆盖整个窗口区域的透明画布,在此示例中我将其称为 MainCanvas。
目的地基本上可以工作,这很奇怪,因为它在屏幕坐标中。源点是问题。如果我使用屏幕坐标,它们起源于窗口的中心。如果我相对于 MainCanvas/Window 使用 TranslateToPoint,那么其中一个点(最接近右下角)几乎看起来是正确的,但其他点从左侧开始。这……很奇怪。如果你们都能对正在发生的事情有所了解,我将不胜感激。好的,这里有一些代码:(我可以提供更多,但我确定问题出在我分享的东西上)
Window window = Window.GetWindow(this);
Point source = TranslatePoint(new Point(5, 5), window),
destPoint = App.MainCanvas.PointFromScreen(GetMousePosition(Window.GetWindow(this)));
if (App.MainCanvas.Children.Contains(_previousLine))
{
App.MainCanvas.Children.Remove(_previousLine);
}
_previousLine = new Line
{ X2 = source.X, X1 = destPoint.X, Y2 = source.Y, Y1 = destPoint.Y, StrokeThickness = 2.5f, Stroke = Brushes.Black };
Debug.WriteLine($"Source: {source} Dest: {destPoint}");
App.MainCanvas.Children.Add(_previousLine);
base.OnGiveFeedback(e);
}
然后是绑定点:
<UserControl.Resources>
<Style TargetType="local:BindPoint">
<Style.Triggers>
<DataTrigger Binding="{Binding PointType, RelativeSource={RelativeSource Self}}" Value="Speed">
<Setter Property="Border.CornerRadius" Value="0"/>
<Setter Property="Border.Background" Value="Goldenrod"/>
</DataTrigger>
</Style.Triggers>
</Style>
</UserControl.Resources>
<Grid>
<Border Name="Border" CornerRadius="25" Width="10" Height="10" Background="LimeGreen" BorderThickness="2" BorderBrush="Black" DragEnter="UIElement_OnDragEnter" DragLeave="UIElement_OnDragLeave" DragOver="UIElement_OnDragOver" MouseMove="UIElement_OnMouseMove" Drop="UIElement_OnDrop" MouseDown="MouseClick">
</Border>
</Grid>
</UserControl>
以及发生这一切的窗口:
<Canvas x:Name="SOCanvas">
<StackPanel>
<uiXaml:BindPoint/>
<uiXaml:BindPoint/>
<uiXaml:BindPoint/>
<StackPanel Orientation="Horizontal">
<Label Content="Long Label "/>
<uiXaml:BindPoint/>
</StackPanel>
</StackPanel>
</Canvas>
</Window>
【问题讨论】: