gpt4 book ai didi

c# - 如何在 WPF 中改变 Canvas 的位置?

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

如何更改 Canvas 的运行时位置,我在其中动态添加了控件(标签、线条)?

我可以使用所有控件缩放 Canvas ,但我无法使用 MouseMove、MouseUp、MouseDown 移动到另一个位置。

<Canvas Name="canvas" Width="1000" Height="400"
Margin="100 0 0 50"
Background="White"
VerticalAlignment="Bottom"
HorizontalAlignment="Center"
MouseWheel="Canvas_MouseWheel"
MouseMove="Canvas_MouseMove"
MouseUp="Canvas_MouseUp"
MouseDown="Canvas_MouseDown">
<Canvas.RenderTransform>
<ScaleTransform x:Name="st" />
</Canvas.RenderTransform>
</Canvas>

我在网上找到这段代码,但对我来说不起作用

bool activated;
Point point;

private void Canvas_MouseDown(object sender, MouseButtonEventArgs e)
{
activated = true;
point = Mouse.GetPosition(canvas);
Mouse.Capture(canvas);
}

private void Canvas_MouseMove(object sender, MouseEventArgs e)
{
if (activated)
{
double top = Canvas.GetTop(canGraph) + Mouse.GetPosition(canvas).Y - point.Y;
Canvas.SetTop(canvas, top);
double left = Canvas.GetLeft(canvas) + Mouse.GetPosition(canvas).X - point.X;
Canvas.SetLeft(canvas, left);
point = Mouse.GetPosition(canvas);
}
}

private void Canvas_MouseUp(object sender, MouseButtonEventArgs e)
{
activated = false;
Mouse.Capture(null);
}

最佳答案

编辑 - 如果没有更多代码,我提供的先前解决方案在元素第一次移动后将无法工作,所以这里有一个更好的解决方案

为了正确工作,canvas 元素必须与其父元素的坐标系对齐,这是我们将 canvas 放在左上角时实现的,如果你不把它放在那里,你必须计算与众不同。

代码隐藏

private void Canvas_MouseDown(object sender, MouseButtonEventArgs e)
{
canvas.CaptureMouse();
}

Stopwatch sw = new Stopwatch();
private void Canvas_MouseMove(object sender, MouseEventArgs e)
{
if (canvas.IsMouseCaptured)
{
translate.X = e.GetPosition(container).X;
translate.Y = e.GetPosition(container).Y;
}
}

private void Canvas_MouseUp(object sender, MouseButtonEventArgs e)
{
canvas.ReleaseMouseCapture();
}

XAML

<Grid Background="Green" x:Name="container">
<Canvas Name="canvas" Width="100" Height="100"
Margin="0 0 0 0"
Background="Purple"
VerticalAlignment="Top"
HorizontalAlignment="Left"
MouseMove="Canvas_MouseMove"

MouseDown="Canvas_MouseDown">
<StackPanel Background="White">

<TextBlock >asdasda</TextBlock>
<TextBlock >cccc</TextBlock>
<TextBlock >aaaaa</TextBlock>
<TextBlock >bbbb</TextBlock>
</StackPanel>
<Canvas.RenderTransform>
<TransformGroup>
<ScaleTransform x:Name="st" />
<TranslateTransform x:Name="translate" />
</TransformGroup>
</Canvas.RenderTransform>
</Canvas>
</Grid>

原始答案


XAML

我会做这样的事情:

  • 添加一个平移变换,为了保持之前的变换,将它放在一个组中

  • 使用 translate transform 以根据鼠标事件的位置移动 Canvas

  • 对于翻译的起点,您可以使用容器中的坐标

代码隐藏:

bool activated;
Point point;

private void Canvas_MouseDown(object sender, MouseButtonEventArgs e)
{
activated = true;
point = e.GetPosition(container);
}

private void Canvas_MouseMove(object sender, MouseEventArgs e)
{
if (activated)
{
translate.X = e.GetPosition(container).X - point.X;
translate.Y = e.GetPosition(container).Y - point.Y;
}
}

private void Canvas_MouseUp(object sender, MouseButtonEventArgs e)
{
activated = false;
}

XAML

<Canvas Name="canvas" Width="100" Height="100"
Margin="0 0 0 0"
Background="Purple"
VerticalAlignment="Stretch"
HorizontalAlignment="Stretch"
MouseMove="Canvas_MouseMove"
MouseUp="Canvas_MouseUp"
MouseDown="Canvas_MouseDown">
<Canvas.RenderTransform>
<TransformGroup>
<ScaleTransform x:Name="st" />
<TranslateTransform x:Name="translate" />
</TransformGroup>
</Canvas.RenderTransform>
</Canvas>
</Grid>

关于c# - 如何在 WPF 中改变 Canvas 的位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33750702/

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