【问题标题】:How do I draw a line on WPF drag and drop to the mouse from the drag source?如何在 WPF 上从拖动源拖放到鼠标上画一条线?
【发布时间】: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>

【问题讨论】:

    标签: c# wpf


    【解决方案1】:

    问题是控件的大小没有得到传播 - 边框中指定的 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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-06-30
      • 2019-04-06
      • 1970-01-01
      • 1970-01-01
      • 2015-09-22
      • 2015-09-25
      • 1970-01-01
      相关资源
      最近更新 更多