开发者

Getting type of control on mouseover

I wanted to get开发者_如何学JAVA the type of the control on mouseover. Please help


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.


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)))
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜