Using C#, can you drag a canvas in WPF?

Posted by TERACytE on Stack Overflow See other posts from Stack Overflow or by TERACytE
Published on 2010-04-25T20:19:04Z Indexed on 2010/04/25 20:23 UTC
Read the original article Hit count: 339

Filed under:
|

Can you drag a canvas in WPF? How do you set the position of the canvas? Here is what I got so far:

/// 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>

/// code behind

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;
        }
    }
}

'X' is not a property of Canvas (i.e."this.ParentCanvas.X"). What do I use to set the position?

© Stack Overflow or respective owner

Related posts about wpf

Related posts about c#