gpt4 book ai didi

c# - 移动 UIElement 时,wpf 相对于窗口的平移点会导致偏移

转载 作者:行者123 更新时间:2023-11-30 20:30:12 26 4
gpt4 key购买 nike

我目前正在开发一个用于使用鼠标拖动 UIElement 的行为类。我以前做过类似的东西,但我试图做到这一点,以便 UIElement 不依赖于某种类型的父级(除了窗口之外)。

我面临的问题是当我尝试平移鼠标和 UIElement(AssociatedObject) 相对于窗口的坐标时,这导致了下面显示的偏移。

尝试添加偏移量来“修复”当前偏移量感觉像是作弊,并且会使创建其余的运动行为变得更加复杂。

目前我正在使用内部包含多个元素的网格来测试dragBehaviorComponentClass。(我用浅蓝色填充网格作为指示器,尽管它与其他形状略有重叠)

the oddly shaped form is the UIElement UIElement: X 788/ Y 345 || Mouse: x 786 / Y 310

我目前只是尝试将 uiElement 的 0,0 位置附加到鼠标点,我还没有处理任何偏移以进一步改进拖动。

代码

public class MouseDragBehaviourComponent : Behavior<UIElement>
{
private TranslateTransform transform = new TranslateTransform();

protected override void OnAttached()
{
AssociatedObject.MouseLeftButtonDown += AssociatedObject_MouseLeftButtonDown;
AssociatedObject.PreviewMouseMove += AssociatedObject_PreviewMouseMove;
AssociatedObject.MouseMove += AssociatedObject_MouseMove;
AssociatedObject.MouseLeftButtonUp += AssociatedObject_MouseLeftButtonUp;
}

private void AssociatedObject_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
AssociatedObject.ReleaseMouseCapture();
}
private void AssociatedObject_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)

{
Point currentPosition = AssociatedObject.TranslatePoint(new Point(0, 0), Window.GetWindow(AssociatedObject));
AssociatedObject.RenderTransform = transform;
AssociatedObject.CaptureMouse();
}

private void AssociatedObject_MouseMove(object sender, MouseEventArgs e)
{
if (AssociatedObject.IsMouseCaptured)
{
Point mousePosition = e.GetPosition(Window.GetWindow(AssociatedObject));
transform.X = mousePosition.X;
transform.Y = mousePosition.Y;
}

}

xaml

<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:UISet}">
<Border >
<e:Interaction.Behaviors>
<bt:MouseDragBehaviourComponent/>
</e:Interaction.Behaviors>
<Grid Background="LightBlue">
<StackPanel>
<Path MouseLeftButtonDown="geometry_MouseLeftButtonDown" Name="geometry" Fill="Blue">
<Path.Data>
<CombinedGeometry GeometryCombineMode="Exclude">
<CombinedGeometry.Geometry1>
<RectangleGeometry RadiusX="2" RadiusY="2" Rect="-5,-50,100,50" />
</CombinedGeometry.Geometry1>
<CombinedGeometry.Geometry2>
<RectangleGeometry RadiusX="2" RadiusY="2" Rect="5,-40,90,40" />
</CombinedGeometry.Geometry2>
</CombinedGeometry>
</Path.Data>
</Path>
<Path MouseLeftButtonDown="geometry_MouseLeftButtonDown" Fill="Red">
<Path.Data>
<PathGeometry>
<PathFigure StartPoint="0,0">
<LineSegment Point="5,6"/>
<LineSegment Point="10,0"/>
<LineSegment Point="0,0"/>
</PathFigure>
</PathGeometry>
</Path.Data>
</Path>
</StackPanel>
</Grid>

最佳答案

代码的设置方式:

transform.X = mousePosition.X;
transform.Y = mousePosition.Y;

获取您移动的物体内部的鼠标位置。这会导致变换远离鼠标,无论距离是多少。要解决这个问题,您需要获取要移动的物体的容器的鼠标位置(将作为主要点),并获取要移动的物体内部光标的鼠标位置。这将充当偏移量。总而言之,代码看起来类似于:

Point mouseContainerLocation = Mouse.GetPosition(container);
Point mouseObjectLocation = Mouse.GetPosition(object);
transform.X = mouseContainerLocation.X - mouseObjectLocation.X;
transform.Y = mouseContainerLocation.Y - mouseObjectLocation.Y;

我在我制作的应用程序中做了类似的事情,这对我有用

关于c# - 移动 UIElement 时,wpf 相对于窗口的平移点会导致偏移,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45167524/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com