Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用C#,您可以在WPF中拖动画布吗?_C#_Wpf - Fatal编程技术网

使用C#,您可以在WPF中拖动画布吗?

使用C#,您可以在WPF中拖动画布吗?,c#,wpf,C#,Wpf,你能用WPF拖动画布吗?如何设置画布的位置?以下是我到目前为止得到的信息: ///xaml <Window x:Class="TestApp.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Height="350" Width=

你能用WPF拖动画布吗?如何设置画布的位置?以下是我到目前为止得到的信息:

///xaml

<Window x:Class="TestApp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Height="350" Width="525"
        WindowStyle="None" ResizeMode="NoResize" AllowsTransparency="True"
        Background="Transparent" Loaded="MainWindow_Loaded">


    <Canvas Name="ParentCanvas" Background="#FF6E798D">
    </Canvas>
</Window>

“X”不是画布的属性(即“this.ParentCanvas.X”)。我用什么来设置位置?

要设置元素的像素位置,元素必须包含在
画布
面板中

然后可以调用
Canvas.SetTop
Canvas.SetLeft

public partial class MainWindow : Window
{
    private Boolean isMouseCapture;

    public MainWindow()
    {
        InitializeComponent();
    }

    void MainWindow_Loaded(object sender, RoutedEventArgs e)
    {            
        this.ParentCanvas.MouseLeftButtonDown += new MouseButtonEventHandler(_MouseLeftButtonDown);
        this.ParentCanvas.MouseLeftButtonUp += new MouseButtonEventHandler(_MouseLeftButtonUp);
        this.ParentCanvas.MouseMove += new MouseEventHandler(_MouseMove);
    }

    void _MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
    {
        this.ParentCanvas.ReleaseMouseCapture();
        isMouseCapture = false;
    }

    void _MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        this.ParentCanvas.CaptureMouse();
        isMouseCapture = true;
    }

    void _MouseMove(object sender, MouseEventArgs e)
    {
        if (isMouseCapture)
        {
            this.ParentCanvas.X= e.GetPosition(this).X;
            this.ParentCanvas.Y = e.GetPosition(this).Y;
        }
    }
}