更改 WPF 中的图像位置

Jos*_*osh 2 c# wpf xaml event-handling

我试图在鼠标悬停时更改图像的位置。我有:

<Image 
    Name="cat" 
    Source="CatRun.bmp" 
    Visibility="Hidden" 
    HorizontalAlignment="Center" 
    VerticalAlignment="Center" 
    Width="100" 
    Height="100"
    UIElement.MouseEnter="cat_MouseEnter"/>
Run Code Online (Sandbox Code Playgroud)

在 XAML 中以及:

private void cat_MouseEnter(object sender, MouseEventArgs e)
{

}
Run Code Online (Sandbox Code Playgroud)

在 C# 中。

如何在画布上专门设置位置?

Phi*_*hil 5

这是一个例子:

<Canvas x:Name="canvas">
    <Rectangle x:Name="rect" Width="20" Height="20" Canvas.Left="10" Canvas.Top="10" Fill="Blue" MouseEnter="RectangleMouseEnter" />
</Canvas>
Run Code Online (Sandbox Code Playgroud)

您需要设置附加属性顶部、左侧(或底部、右侧)

    private void RectangleMouseEnter(object sender, MouseEventArgs e)
    {
        Canvas.SetTop(rect, 50);
        Canvas.SetLeft(rect, 50);
    }
Run Code Online (Sandbox Code Playgroud)