C# 在父级的 MouseHover/MouseLeave 上显示/隐藏元素
在 C# 中,我们有以下内容:
- 一个
UserControl
,其中包含一个PictureBox
和一个不可见的FlowPanel
。
我想要实现的目标:
- 当
UserControl
悬停时 (MouseHover
),不可见的FlowPanel
将设置为visible = 正确。当鼠标离开时
UserControl
或FlowPanel
,FlowPanel
应设置visible = 假
。
在 UserControl
上使用 MouseLeave
并不能完成这项工作,因为当鼠标进入 FlowPanel
时会触发此事件。当鼠标离开FlowPanel
时隐藏FlowPanel
可以做到这一点,但是有问题(有时MouseLeave
被触发,有时不会)。
解决这个问题的最佳方法是什么?
In C#, we have the following:
- A
UserControl
containing aPictureBox
and an invisibleFlowPanel
.
What I want to achieve:
- When the
UserControl
is hovered
(MouseHover
), the invisibleFlowPanel
will be set tovisible =
. When the mouse leaves the
trueUserControl
orFlowPanel
, theFlowPanel
should be setvisible =
.
false
Using MouseLeave
on UserControl
doesn't do the job, because this event is triggered when the mouse enters FlowPanel
. Hiding the FlowPanel
when the mouse leaves FlowPanel
does it, but is buggy (sometimes MouseLeave
is triggered, sometimes not).
What's the best way to fix this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我在我的一个表单上做了类似的事情,
在第一个事件中执行 if(contorl.Opacity = 1.0)
i did somthing similar on one of my forms
do a if(contorl.Opacity = 1.0) inside your first event
在
FlowPanel.MouseLeave
没有触发的情况下,UserControl.MouseLeave
不是也被触发了吗?我认为隐藏这两个事件可能会奏效。In the case when
FlowPanel.MouseLeave
isn't triggered, isn'tUserControl.MouseLeave
triggered? I suppose that hiding on both events may do the trick.这是一个常见的 UI 问题。鼠标事件作为样本出现,因此可能会丢失某些像素位置,并且控件无法获取鼠标释放事件。
一种不太好的工作方式是,当在 Control 内检测到 MouseHover 时设置某种形式的计时器,并定期轮询光标(例如 342 毫秒)。
This is a common UI problem. Mouse events come up as samples so it's possible that some pixel positions are missed and a control doesn't get the mouse up event.
A not so nice way that works is setting up some form of Timer when MouseHover is detected inside the Control and poll for the cursor in regular intervals (such as 342ms).