在 .NET 2.0 中隐藏与 UserControl 关联的工具箱的最佳方法?
我最近遇到了以下情况。 我有一个可以显示无模式工具箱的UserControl
。 如果用户已显示工具箱,我想随着 UserControl
本身变得不可见或可见而相应地隐藏和显示它。 UserControl
可以嵌入任意数量的父容器中,例如 GroupBox
或 TabPage
,这可以影响 >UserControl
实际上是可见的,尽管它自己的 Visible
属性为 True
。
在 WPF 中,似乎我可以使用 UserControl
的 IsVisibleChanged
事件来处理此问题。 WinForms 有等效的吗? .NET 2.0 中的通用解决方案是什么?
编辑:这是我得出的解决方案。 有更好的解决方案吗?
public partial class MyControl : UserControl
{
private List<Control> _ancestors = new List<Control>();
private bool _isVisible = false;
public MyControl()
{
InitializeComponent();
ParentChanged += OnParentChanged;
VisibleChanged += OnVisibleChanged;
}
private void OnParentChanged(object sender, EventArgs e)
{
foreach (Control c in _ancestors)
{
c.ParentChanged -= OnParentChanged;
c.VisibleChanged -= OnVisibleChanged;
}
_ancestors.Clear();
for (Control ancestor = Parent; ancestor != null; ancestor = ancestor.Parent)
{
ancestor.ParentChanged += OnParentChanged;
ancestor.VisibleChanged += OnVisibleChanged;
_ancestors.Add(ancestor);
}
}
private void OnVisibleChanged(object sender, EventArgs e)
{
bool isVisible = Visible;
foreach (Control c in _ancestors)
{
if (!c.Visible)
{
isVisible = false;
break;
}
}
if (isVisible != _isVisible)
{
_isVisible = isVisible;
// Control visibility has changed here
// Do something
}
}
}
I've recently come across the following situation. I have a UserControl
that can show a modeless toolbox. If the user has shown the toolbox, I'd like to hide and show it appropriately as the UserControl
itself becomes invisible or visible, respectively. The UserControl
can be embedded in an arbitrary number of parent containers, such as a GroupBox
or TabPage
, which can influence whether or not the UserControl
is actually visible despite its own Visible
property being True
.
In WPF, it seems like I could use the IsVisibleChanged
event of the UserControl
to handle this. Is there an equivalent for WinForms? What would a general solution in .NET 2.0 be?
EDIT: Here is the solution that I arrived at. Is there a better solution?
public partial class MyControl : UserControl
{
private List<Control> _ancestors = new List<Control>();
private bool _isVisible = false;
public MyControl()
{
InitializeComponent();
ParentChanged += OnParentChanged;
VisibleChanged += OnVisibleChanged;
}
private void OnParentChanged(object sender, EventArgs e)
{
foreach (Control c in _ancestors)
{
c.ParentChanged -= OnParentChanged;
c.VisibleChanged -= OnVisibleChanged;
}
_ancestors.Clear();
for (Control ancestor = Parent; ancestor != null; ancestor = ancestor.Parent)
{
ancestor.ParentChanged += OnParentChanged;
ancestor.VisibleChanged += OnVisibleChanged;
_ancestors.Add(ancestor);
}
}
private void OnVisibleChanged(object sender, EventArgs e)
{
bool isVisible = Visible;
foreach (Control c in _ancestors)
{
if (!c.Visible)
{
isVisible = false;
break;
}
}
if (isVisible != _isVisible)
{
_isVisible = isVisible;
// Control visibility has changed here
// Do something
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不幸的是,当使用多个表单或控件时,诸如
Visible
之类的属性要求您手动设置它们。 遍历其属性依赖于其他控件的属性的所有控件是一件痛苦的事情,而且有些乏味。如果您设置 UserControl.Tag 来引用您的工具箱会怎么样?
Unfortunately, when working with multiple forms or controls, properties like
Visible
require that you set them manually. It's a pain, and somewhat tedious to go through all controls whose properties are dependent on other controls' properties.What if you set the UserControl.Tag to reference your toolbox?