I'm working in a WPF application in C#, based on quite a complicated XAML, and I'm trying to modify the location on the canvas of a component. Now my component is gone, which might have two reasons:
- either I did something wrong and my component is gone indeed.
- or I did nothing wrong and my component is hidden behind some other component.
You might say "That's easy! Just put it somewhere where there are no other components." but that's the problem: due to some issues in the XAML I can't show it in design view because it makes my Visual Studio crash.
"So what? Just have a look at the rest of your components and try to find out." but also this is not easy, due to the myriad of components on my screen.
When launching the application, I see some holes in the canvas, but I can't find out the X and Y coordinates (Canvas.Left
and Canvas.Top
values) of those locations.
In order to find those, I would like to have some label on my canvas, which shows X and Y coordinates of the mouse, whenever I move my mouse over that canvas.
Although this seems relatively simple, due to my lack of XAML knowledge I have no idea how to do this.
Does anybody know how to add a Label
on a XAML Canvas
, reacting on ±MouseOver
events and showing Canvas.Left
and Canvas.Top
values?
Attempt 1:
I can confirm that this is not working: (OnMouseOver
is not known)
<Canvas Height="3000" Width="7000"
OnMouseOver="XY_Pos.Text='X : [' + Self.Left + '], Y : [' + Self.Top + ']'">
<Label x:Name="XY_pos" Canvas.Top="100" Canvas.Left="100"/>
Attempt 2:
Also this is not working:
In the xaml:
<Canvas Height="3000" Width="7000"
ToolTipOpening='Canvas_ToolTipOpening'
ToolTipService.ToolTip='Testing'>
<Label x:Name="XY_pos"
Canvas.Top="100" Canvas.Left="100"/>
In the corresponding xaml.cs
file:
private void Canvas_ToolTipOpening(object sender, ToolTipEventArgs e)
{
e.Handled = true;
XY_pos.Content = $"X=[{((Canvas)sender).Left}], Y=[{((Canvas)sender).Top}}]";
}
This latter code recognises XY_pos
, but generated CS1061
errors on the Left
and Top
properties.
Not only that, but I'm debugging this function: now it seems that sender
is of type System.Windows.Controls.Canvas
and that type does not have a Left
or a Top
property. Is this correct? In order to position a control on a Canvas
you need to fill in the Canvas.Left
and Canvas.Top
properties, but a Canvas
object does not even have a Left
and a Top
property???
Thanks in advance
XAML:
<Canvas x:Name="canvas1" MouseMove="hit spacebar here then select from the dropdown create new event handler">
C#: find the new event handler method with the name of the one created in the MouseMove property in the XAML.
canvas1_MouseMove(object sender, e MouseEventArgs) method:
{
var mousePos = e.GetPosition(canvas1);
canvas1.ToolTip = mousePos.X.ToString() + " " + mousepos.Y.ToString();
}
this should make a tooltip appear whenever the mouse is over the canvas that has the X and Y coordinates of the mouse relative to the canvas. at may need some minor tweaks but that's worked for me in the past. Good Luck!
P.S can confirm this will work and here is the code I used to test it and will round the values to whole number rather than 5 decimal points:
private void canvas1_MouseMove(object sender, MouseEventArgs e)
{
var mousePos = e.GetPosition(canvas1);
canvas1.ToolTip = "X = " + ((int)mousePos.X).ToString() + " || Y = " + ((int)mousePos.Y).ToString();
}
Also you can set the element position in the codebehind with
UIElement uIElement = new UIElement();
canvas1.Children.Add(uIElement);
Canvas.SetLeft(uIElement, 100);
Canvas.SetTop(uIElement, 100);