tags:

views:

43

answers:

2

I wanted to get the type of the control on mouseover. Please help

A: 

Add mouse_enter event to the control.

You can get the type with a line of code as follow

var x = sender.GetType();

You can then compare it using something like:

if (x.Equals(typeof(TreeView)))
Johannes
Thank for the reply JohannesHowever i have multiple controls in my container say Ellipse, Rectangle, TextBlock etc... and i should be able to get the type in one function
Sathish
+1  A: 

You can get the type of the UIElement over which the mouse is currently moving using the MouseMove event. Since this is a bubbling event you can attach a handler to the container such as a Canvas.

The UIElement over which the mouse is currently moving can be aquired from the the MouseEventArgs OriginalSource property.

Hence to determine the type over which the mouse is moving you could use code like this:-

void Canvas_MouseMove(object sender, MouseEventArgs e)
{

    Type currentType = e.OriginalSource.GetType();
    // Make decisions based on value of currentType here
}

However you need be careful, MouseMove fires frequently as the user moves the mouse so you might want to defer any heavy work until there is some time period after the last mouse move.

There is unfortunately no bubbling mouse over event.

The other alternative is to attach the same MouseEnter handler to each child UIElement you add to the Canvas. You could use sender instead of e.OriginalSource in that case. You would have to be careful to remove the handler if the element is removed from the Canvas, else you can create what would appear to be a memory leak.

AnthonyWJones