繁体   English   中英

如何在WPF上画一条线从拖拽源拖拽到鼠标上?

[英]How do I draw a line on WPF drag and drop to the mouse from the drag source?

我在翻译点时遇到了一些问题。 我想从控件中心到鼠标画一条线。 我目前正在使用 GiveFeedback 事件,主要是因为我能够通过它获得一些结果——我还有其他处理程序也在使用。 请注意,我有一个透明的 canvas 覆盖整个 window 区域,在此示例中我将其称为 MainCanvas。

目的地基本上是有效的,这很奇怪,因为它在屏幕坐标中。 源点是问题。 如果我使用屏幕坐标,它们起源于 window 的中心。 如果我相对于 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>

而这一切都发生在 Window 上:

    <Canvas x:Name="SOCanvas">
    <StackPanel>
        <uiXaml:BindPoint/>
        <uiXaml:BindPoint/>
        <uiXaml:BindPoint/>
<StackPanel Orientation="Horizontal">
    <Label Content="Long Label                           "/> 
    <uiXaml:BindPoint/>

</StackPanel>
    </StackPanel>
    </Canvas>
</Window>

问题是控件的大小没有得到传播 - 边框中指定的 10x10 没有应用于 MainWindow.xaml 或其他地方。 因此,源点来自父级传递控制权的任何边界。 创建一个设置大小的默认样式解决了这个问题。

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:local="clr-namespace:UITestNon_Avalonia.UI_Xaml"
                    >
<Style x:Key="{x:Type local:BindPoint}" TargetType="local:BindPoint">
    <Setter Property="Width" Value="10"/>
    <Setter Property="Height" Value="10"/>
</Style>
</ResourceDictionary>

暂无
暂无

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

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