Moving a control on a Canvas at run time

Hi,

I'm trying to produce a simple designer in Xaml, for a test app I've produced the following Xaml:

<Windowx:Class="xamlWindowsApp.frmDesigner"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"Title="xamlWindowsApp"Height="450"Width="582"xmlns:my="clr-namespace:System;assembly=mscorlib">

<DockPanelMargin="1,1,2,2"MinHeight="50"MinWidth="50"Name="dockPanel1"LastChildFill ="True">

<CanvasMouseLeftButtonUp ="OnLeftButtonUp">

<LabelMouseLeftButtonDown ="OnLeftButtonDown"MouseLeftButtonUp ="OnLeftButtonUp"MouseMove ="OnMouseMove"HorizontalAlignment="Right"Name="label1"Width="60"BorderBrush="#FFC0C0C0"BorderThickness="3,3,3,3"Height="30"VerticalAlignment="Bottom"Canvas.Left="40"Canvas.Top="10">Movable</Label>

</Canvas>

</DockPanel>

</Window>

I have two question:

1. How do I get the location of the mouse on the canvas?

2. How do I change the Labels canvas properties so that I can get it to track the mouse within the canvas.

Thanks in advance for any help

AndyH

[6794 byte] By [LokiDBA] at [2007-12-24]
# 1

I have found how to get the mouse location on the canvas using:

System.Windows.Input.Mouse.GetPosition(this)

Does anyone one know if there is a better or Best Practice way of doing this.

Thanks

Andy

LokiDBA at 2007-8-31 > top of Msdn Tech,Visual Studio Orcas,WPF Designer (Cider)...
# 2

Hi Andy,

This forum is for the WPF Designer for Visual Studio. For questions about the WPF runtime behaviors & usage, please post to the WPF Forum (http://forums.microsoft.com/MSDN/ShowForum.aspx?ForumID=119&SiteID=1). There are many smart eyeballs there, I'm certain you'll find some suggestions on run-time mouse capture & layout.

thanks

chad

chadr-msft at 2007-8-31 > top of Msdn Tech,Visual Studio Orcas,WPF Designer (Cider)...
# 3

Thanks, will do.

Found a possible answer, you can use static methods

Canvas.SetTop( object, value)

Canvas.SetRight(object, value)

Canvas.SetLeft(object, value)

Canvas.SetBottom(object, value)

But I'll definatly check the other forum out for the best practice.

Thanks again.

Andy

LokiDBA at 2007-8-31 > top of Msdn Tech,Visual Studio Orcas,WPF Designer (Cider)...
# 4

Depending on what you are trying to accomplish, you may be interested in the InkCanvas class which provides an edit mode that allows the end user to select and move its child elements.

http://www.windojitsu.com/blog/tictactoefx.html

http://windowssdk.msdn.microsoft.com/en-us/library/system.windows.controls.inkcanvas.aspx

JimNakashima-MSFT at 2007-8-31 > top of Msdn Tech,Visual Studio Orcas,WPF Designer (Cider)...
# 5

u can use

Point startPoint = e.MouseDevice.GetPosition(CanvasName);

and store it in Point.

hope this will work.. I have done this and that is working. :)

VinodSa at 2007-8-31 > top of Msdn Tech,Visual Studio Orcas,WPF Designer (Cider)...
# 6

Hi

Dude

In Mouse event

point p=e.GetPosition(canvasId);

p.X you ll get X-axis point

p.Y you ll get Y-axis point

This may help you

Thanks

vishwa

Balekai at 2007-8-31 > top of Msdn Tech,Visual Studio Orcas,WPF Designer (Cider)...

Visual Studio Orcas

Site Classified