如何在UIElement下获取按钮 [英] How to get the button under a UIElement
本文介绍了如何在UIElement下获取按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在鼠标下找到按钮,但是它上面还有另一个UIElement.在我的表单中,它是一个矩形,我需要它,因此无法将其删除.
我可以像这样在鼠标下获得第一个UIElement:
I want to get the button under mouse, but there is another UIElement above it. In my form it''s a Rectangle,I need it,so it couldn''t be remove.
I can just get the first UIElement under mouse like this:
UIElement MyButton = GetElementUnderMouse<UIElement>();
public static T FindVisualParent<T>(UIElement element) where T : UIElement
{
UIElement parent = element;
while (parent != null)
{
var correctlyTyped = parent as T;
if (correctlyTyped != null)
{
return correctlyTyped;
}
parent = VisualTreeHelper.GetParent(parent) as UIElement;
}
return null;
}
public static T GetElementUnderMouse<T>() where T : UIElement
{
return FindVisualParent<T>(Mouse.DirectlyOver as UIElement);
}
<然后返回="=" the =" how =" to =" go =" on =" find =" mode ="hold">非常感谢有人帮助我获得按钮.
<then returns="" the="" how="" to="" go="" on="" find="" mode="hold">Thanks very much if anyone helps me get the button.
推荐答案
您要达到什么目的?
如果要处理显示和隐藏按钮,则可以使用以下代码
what are u trying to achieve?
if its to deal with showing and hiding the button u may use below code
button1.Visibility = Visibility.Hidden;// to hide,just as if its under
button1.Visibility = Visibility.Visible;//to show it
如果您想隐藏隐藏按钮的属性,则
鼠标悬停在
if u wanna get the properties of the button with itself hidden then
on mouse over
button1.Visibility = Visibility.Visible;
button1.Opacity=0;
这篇关于如何在UIElement下获取按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文